Download

Spawned Weapon Pawn is not visible on client

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.

Okay, I think I have figured it out after some hours of testing (multiplayer debugging is no fun ;)). The weapon gets spawned by the server and is attached to the server’s character. So its position is moved in the scene so that the weapon fits in the character’s hand. Somehow the mesh position change is replicated to the client and if the client tries to attach the weapon to itself, it is moved again and so the weapon is renders at another position in the world.

I do not fully understand how this can happen and why using a delay is a workaround for this problem (the position change should also happen). Maybe some of you has a good explanation for this or maybe you still have a hint for me how to solve this problem in a good way. Currently it works quite well, if I simply use SetWorldTransform to reset the position of the weapon before getting started. But maybe it is not the best way to do so and there is something wrong in my idea of spawning weapons. I could not find a similar reset in the ShooterGame and obviously there are none of these problems. So maybe I am doing things wrong.