OnRep nullptr problem


void ATOCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const

    DOREPLIFETIME(ATOCharacter, CurrentWeapon);

	DOREPLIFETIME(ATOCharacter, KnifeWeaponSlot);
	DOREPLIFETIME(ATOCharacter, PistolWeaponSlot);
	DOREPLIFETIME(ATOCharacter, SMGWeaponSlot);
	DOREPLIFETIME(ATOCharacter, RifleWeaponSlot);
	DOREPLIFETIME(ATOCharacter, GranadeWeaponSLot);


void ATOCharacter::OnRep_SMG()
	UE_LOG(LogTemp, Warning, TEXT("SMG changed"));
	if (!SMGWeaponSlot) EquipBestWeapon();


UPROPERTY(ReplicatedUsing = OnRep_SMG)
	ATOWeapon* SMGWeaponSlot;

	void OnRep_SMG();

It works for every case (server equips a new valid weaponactor), but when the weapon gets unequiped (Destroy() + SMGWeaponSlot = nullptr) it does not call the OnRep method on the client.

Is this normal behavior?


So… You want to replicate something that ins’t there anymore.

I want the client to notice that there isn’t something anymore :smiley:

I would expect it to replicate regardless. A nullptr value still represents a value change. Is there any difference if you don’t call Destroy()?

This isn’t normal behavior. When the variable changes to nullptr on the server, it should replicate down to the client and the OnRep should fire.

Is the destroy event also executed on the client? If the client himself sets the pointer to nullptr and then the server’s change is received, OnRep_ won’t fire because the client doesn’t see it as a change in value.