I’ve been going through the shooter game example that epic provides that shows an example of a FPS based on C++. However the below code is stumping me.
How does an onrep call a function that sets that sets the variable that should trigger the onrep again, without resulting in an infinite loop. Recreating the below code in a blueprint does indeed cause an infinite loop.
Does c++ handle onrep differently than blueprints do?
ShooterCharacter.h
/** currently equipped weapon */
UPROPERTY(Transient, ReplicatedUsing = OnRep_CurrentWeapon)
class AShooterWeapon* CurrentWeapon;
/** updates current weapon */
void SetCurrentWeapon(class AShooterWeapon* NewWeapon, class AShooterWeapon* LastWeapon = NULL);
/** current weapon rep handler */
UFUNCTION()
void OnRep_CurrentWeapon(class AShooterWeapon* LastWeapon);
ShooterCharacter.cpp
void AShooterCharacter::**OnRep_CurrentWeapon**(AShooterWeapon* LastWeapon)
{
**SetCurrentWeapon**(CurrentWeapon, LastWeapon);
}
void AShooterCharacter::**SetCurrentWeapon**(AShooterWeapon* **NewWeapon**, AShooterWeapon* LastWeapon)
{
AShooterWeapon* LocalLastWeapon = NULL;
if (LastWeapon != NULL)
{
LocalLastWeapon = LastWeapon;
}
else if (NewWeapon != CurrentWeapon)
{
LocalLastWeapon = CurrentWeapon;
}
// unequip previous
if (LocalLastWeapon)
{
LocalLastWeapon->OnUnEquip();
}
**CurrentWeapon = 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(LastWeapon);
}
}