equiping player weapons

Options
void Start () { pv = GetComponent<PhotonView>(); viewid = pv.viewID; } void Update () { if (Input.GetKeyDown(KeyCode.Space)) { pv.RPC("equipWeapon", PhotonTargets.All, viewid); }[PunRPC] public void equipWeapon(int id) { if (id != viewid) return; // equip weapon }

I am trying to equip playerweapons through network.
Problem is both my players are equiping weapons, even though i am comparing thier view id
Any1 knows how to do this??

Comments

  • Hi @sandy410,

    before sending the RPC you need to check the pv.isMine condition otherwise each client will send the RPC if Space key is pressed. Basically it is a good idea to check this condition at the beginning of the Update() function to avoid unnecessary processing.
    public void Update()
    {
        if (!pv.isMine)
        {
            return;
        }
    
        // do stuff
    }
  • sandy410
    Options
    Thanks this works, but still a problem
    Ppl joining later cannot see my current weapon.
    How do i fix this??
  • One option you have is to implement OnPhotonPlayerConnected(PhotonPlayer newPlayer) callback and notify the newly connected player directly using another RPC by using the PhotonPlayer as parameter when sending the RPC, e.g. RPC("Method", newPlayer, activeWeapon);
  • sandy410
    Options
    I dont get it.
    lets say i have 3 players in room. 1 person joins room. Then i have to use OnPhotonplayerConnected on all three previous player and send thier active weapon??
    Currently i have
    public void equipWeapon(int id,int weaponno)
    {
    //equips weapon
    }
    so OnPhotonPlayerConnected(PhotonPlayer newPlayer)
    {
    RPC("equipWeapon",newplayer,activeWeapon);
    }
    will this work
    Its Confusing
  • Yes, this should work. At least this is one option.

    If you use OnPhotonSerializeView to synchronize player's position and rotation, you can also use this in order to synchronize the current weapon. This however results in more used bandwidth with unimportant information; e.g. if the player doesn't change the weapon for a few seconds or even minutes, the current weapon will be synchronized nevertheless und you will write always the same data to the stream.
  • Majicpanda
    edited June 2017
    Options

    Yes, this should work. At least this is one option.

    If you use OnPhotonSerializeView to synchronize player's position and rotation, you can also use this in order to synchronize the current weapon. This however results in more used bandwidth with unimportant information; e.g. if the player doesn't change the weapon for a few seconds or even minutes, the current weapon will be synchronized nevertheless und you will write always the same data to the stream.

    It's my understanding that if your PhotonView is set to Reliable DeltaCompressed that you will not waste bandwidth by constantly sending it.

    From docs:
    "Reliable Delta Compressed will compare each value of an update to its predecessor. Values that did not change are skipped to keep traffic low. The receiving side simply fills-in the values of previous updates. Anything you write via OnPhotonSerializeView atomatically gets checked and compressed this way. If nothing changed, OnPhotonSerializeView doesn't get called on the receiving clients. The "reliable" part here takes some toll, so for small updates, the overhead should be considered."

    Is there something to be interpreted from this that isn't as it reads? I do agree though, sending reliable data should be minimalized as much as possible.

    I assume all Buffered RPCs stack infinitely for weapon changes, as example.. and it wouldn't just be the latest RPC of that method? If it was the latest only, then it would be a great way to implement for new players. I think you're correct for Photon anyway, to use some custom logic to update all clients to the "latest data" via OnPhotonPlayerConnected per script.. just a bit more work than Forge, Bolt, even UNET which have SyncVars etc that auto fire for all new connections.