I change the CurrentWeapon variable from the Server (because I want the server to change client’s variable).
void AWeapon::Fire()
{
// HERE the call for the ServerRPC function
if (Role < ROLE_Authority)
ServerFire();
else
{
// Weapon-specific shooting
FireWeapon();
// At the end of the shooting, decrease ammunitions
DecreaseAmmo();
// Check if out-of-ammo. If yes, switch weapon
if (MyOwner->GetAmmo(WeaponConfig.PrimaryAmmo) == 0)
{
StopFire();
MyOwner->ForceWeaponSwitch();
return;
}
... .... ...
}
}
MyOwner is the PlayerCharacter owning the Weapon and ForceWeaponSwitch() is a PlayerCharacter function which simply changes the CurrentWeapon variable
The problem is that, after the ServerRPC call executes, the CurrentWeapon variable is still the same, it doesn’t change.
But why?
If I play in the Editor with 2 windows (one is the listen server player and the other one is a generic client) the variable changes in the ListenServer player, but doesn’t change in the other player (which is the client).
Are you sure the RPC is executed on the server (for the non-host player)?
Thanks, but it would be helpful to see all functions involved in the RPC, like ServerFire for example, if I remember correctly, there is ServerFire_Implementation and ServerFire_Validate as well.
AWeapon is the class of the Weapons as well as the class I call ForceWeaponSwitch() from. The player has an Inventory of Weapons, which is already replicated and has been used for other gameplay algorithms.
Could you try changing the variable type to a simple type like bool or int?
We could then see if this is related to the variable being a reference to UObject.
Ok in the MyCharacter::ForceWeaponSwitch() function (I remind you, called by the AWeapon class) I change a boolean that determines whether the character can move or not. Well, that works. That boolean changes value succeffully.
But still I can’t get the Weapon switch to happen.
AWeapon* , by the way, is a pointer to an object of AWeapon class (which derives from AActor)
So a bool change is replicated but the AWeapon* is not.
I would then guess that the instance of AWeapon is somehow not replicated properly on the client, but you said it is.
Therefore I have no other ideas.
If this is an important problem for you, you can try to isolate the issue:
Create an example with just the one class that needs to replicate a pointer to another actor. If the problem persists it may be an engine bug, if not, look at the difference between the minimal example and your project.