Sending data when Player properties changes

Options
So lately i've been trying to work on weapon switching,
what i did is that i made the player change the weapon locally first (offline), then i change the player Custom Properties which is "currentWeapon" to the weapon index in the array and then in the server i used the Callback method (OnPhotonPlayerPropertiesChanged) to send the weapon index of the player who switched the weapon to all players
I'm still trying to figure out a way if you can help me out with an idea to send all players the function weapon switch as an RPC.. Thanks :)

here's the code:
void Start()
{
WeaponHash = new Hashtable() { { "currentWeapon", Array.IndexOf(Weapons, currentWeapon) } } ;
PhotonNetwork.player.SetCustomProperties( WeaponHash );
}

void SwitchWeapon(PlayerWeapon weapon)
{
//PhotonNetwork.player.SetCustomProperties()
foreach (PlayerWeapon w in Weapons)
{
if(weapon != w)
{
w.gameObject.SetActive(false);
}
else
{
WeaponHash["currentWeapon"] = Array.IndexOf(Weapons,weapon);

if(PhotonNetwork.player.IsLocal)
{
PhotonNetwork.player.SetCustomProperties(WeaponHash);
}

currentWeapon = weapon;
weapon.gameObject.SetActive(true);
}

}
}


void OnPhotonPlayerPropertiesChanged(object[] hash)
{
PhotonPlayer player = hash[0] as PhotonPlayer;
Hashtable data = hash[1] as Hashtable;

if (player.ID == PhotonNetwork.player.ID)
return;
Debug.Log(Weapons[(int)data["currentWeapon"]].name);
//SwitchWeapon(Weapons[(int)data["currentWeapon"]]);
}

Comments

  • Hi @SmartLessGamer,

    if you haven't done this already I would strongly recommend you reading the RPCs and Raise Event guide which provides a simple example and gives you an idea how things work.

    In your case the client can for example call photonView.RPC("SwitchWeapon", PhotonTargets.All, newWeapon); where photonView is the attached PhotonView component (make sure to check isMine condition before sending any kind of RPC), "SwitchWeapon" is the function which all clients should execute, PhotonTargets.All describes the receiver group and newWeapon is some kind of identifier which weapon should be activated. This can be an unique ID, the unique name or something like this. Make sure not trying to send GameObjects with RPCs since this won't work.

    The receiving clients can now use the identifier in order to activate the new weapon.
  • Thank you @Christian_Simon
    But i've already done, i was so stupid, i forgot to check if localPlayer is only the one controlling input
    Thanks again :smile: