Odd mesh attachment issue

I’m using code from ShooterGame as a reference for developing a top-down shooter.

The issue I’m having is that the first weapon on the client is not attaching to the character correctly - its attaching but floating well above his head rather than at the attachment point. The strange thing is that if I use the same weapon multiple times in the DefaultInventory, all subsequent weapons work when I switch weapons, but the first one is still not attached correctly even when I scroll back to it to equip it.

This happens only on the client.

Any suggestions on where I can start to track this down?

Cheers

I had a very similar problem in shooter game a few weeks ago. Its fixed in the latest version, but that’s not released to rocket users. It sounds similiar to what you are seeing, so I’ll just explain what that problem was and how I fixed it.

(I’m just going to refer to ShooterGame source for this)

My guess is AShooterWeapon::AttachMeshToPawn is being called on the client when MyPawn is still NULL. This causes the attachment to not happen. Basically, MyPawn is a replicated property on weapons. CurrentWeapon is a replicated property on pawns. when CurrentWeapon is replicated, the pawn tries to attach the CurrentWeapon (through like OnEquip and some other functions). But if the weapons MyPawn hasn’t replicated yet, the weapon can’t attach itself.

The fix I made was just to make sure that the weapon’s MyPawn was explicitly set by the pawn before calling OnEquip.

The code in shooter game changed to this:

void AShooterCharacter::SetCurrentWeapon(class AShooterWeapon* NewWeapon)
{
	// unequip previous
	if (LocalCurrentWeapon)
	{
		LocalCurrentWeapon->OnUnEquip();
	}

	CurrentWeapon = NewWeapon;
	LocalCurrentWeapon = NewWeapon;

	// equip new one
	if (NewWeapon)
	{
		NewWeapon->SetOwningPawn(this);	// Make sure weapon's MyPawn is pointing back to us. During replication, we can't guarantee APawn::CurrentWeapon will rep after AWeapon::MyPawn!
		NewWeapon->OnEquip();
	}

The call to NewWeapon->SetOwningPawn was the change. I added the SetOwningPawn function:

void AShooterWeapon::SetOwningPawn(AShooterCharacter* NewOwner)
{
	if (MyPawn != NewOwner)
	{
		Instigator = NewOwner;
		MyPawn = NewOwner;
		// net owner for RPC calls
		SetOwner(NewOwner);
	}	
}

Hopefully that gives you some ideas of where to look. My guess is, if it isn’t that exact problem, its a problem where an RepNotify is being called on your character/pawn that relies on not-yet-replicated property on the weapon class.

Hey Dave,

My first thought was something not being replicated early enough, but stepping through and checking everything being valid was the first step I took and MyPawn seemed to be valid at the time. I also implemented your fix and it didn’t make a difference (as MyPawn for the ShooterWeapon is never null when trying to equip).

The part that is confusing to me is that every subsequent weapon I have in my Inventory array works fine, and even when I switch back to the first one, it doesn’t attach properly, even though AttachMeshToPawn() is called when CurrentWeapon is replicated. Its also moving with the played, and bobbing up and down like it should with the animation. But its just situated in a weird place. So I’m still totally confused.

Cheers

Hmm yeah that does sound really strange. Just some ideas/questions:

  • Does it happen with any weapon? Is it always the first? If you change weapon order / initial weapon, does it still happen?

  • Since you say if you have multiple copies of the same weapon, can you break in with the debugger and inspect the good/bad versions? I would look at the weapon’s root component and look at things like ‘AttachParent’, ‘AttachSocketName’, ‘RelativeLocation’ and ‘RelativeRotation’. Compare these and see if they are the same or not.

Nothing really makes sense, since you say if you unequip and reequip that the attachment is still broken.