Hello,
I have a problem with spawning my weapon mesh for my character. I had a look at the ShooterGame to get some inspiration, but tried to simplify the system for my prototype a lot.
So currently I have a default weapon class that should be spawned and equipped at the start of the game. But if I try to use it with a dedicated server (or run the game on more than one client) the weapon is not visible within the hands of my character. It only works, if I attach the weapon after a small delay (see below). That’s really strange.
My PostInitializeComponents method of the character class looks like.
Super::PostInitializeComponents();
if ( Role == ROLE_Authority )
{
print( "Spawn Weapon" );
FActorSpawnParameters spawnInfo;
spawnInfo.bNoCollisionFail = true;
AABWeapon* newWeapon = GetWorld()->SpawnActor<AABWeapon>( defaultWeapon, spawnInfo );
this->EquipWeapon( newWeapon );
}
Equip weapon does call SetCurrentWeapon (if it is on the server otherwise SetCurrentWeapon will be called if currentWeapon is replicated):
{
currentWeapon = newWeapon;
if( currentWeapon )
{
currentWeapon->SetOwningPawn( this );
currentWeapon->AttachMeshToPawn();
}
}
SetOwningPawn stores a (replicated) pointer to the current owner in the weapon class.
AttachMeshTo Pawn binds the weapon mesh to the character:
if( myPawn )
{
DetachMeshFromPawn();
FName AttachPoint = myPawn->GetWeaponAttachPoint();
USkeletalMeshComponent* UseWeaponMesh = GetWeaponMesh();
USkeletalMeshComponent* UsePawnMesh = myPawn->GetPawnMesh();
UseWeaponMesh->AttachTo( UsePawnMesh, AttachPoint );
UseWeaponMesh->SetHiddenInGame( false );
}
This only works if I don’t use more than one client or don’t use a dedicated server. But the really strange thing ist, that it works if I adjust the SetCurrentWeapon method to:
{
currentWeapon = newWeapon;
if( currentWeapon )
{
currentWeapon->SetOwningPawn( this );
GetWorldTimerManager().SetTimer( currentWeapon, &AABWeapon::AttachMeshToPawn, 1.f, false );
}
}
Then the weapon appears about one second after the game has started in the hands on my actor. But even if it works, I wonder why and if it is possible to get the weapon spawned immediately in the hands of my character.
Does anyone know, what could be the problem that the weapon is invisble (or just not there), if I don’t use the timed method call?
Thanks in advance.