Syncing object state to newly joined player

Options
Hello, I've been using PUN for the last few weeks and I've been enjoying how easily it is to work with. Up until now I've been able to get everything working, but I'm having some doubts about the actual implementation.

The following scenario is a big part of my application and I don't really know how to efficiently implement it.

When a player grabs an object, that object's colliders should be disabled, when they release it, the colliders should get reenabled.

Right now I use a buffered RPC to enable/disable the colliders, otherwise, new players that join when an object is grabbed don't know about this change. I've not run into problems yet, but these buffered RPC calls seem redundant. Is there a cleaner way of syncing object states? This scenario is something I run into frequently (setting localPosition, setting script values, ...) so I'm worried it might cause problems in the long run.

Are buffered RPCs really the way to go?
Perhaps making a function that syncs these values when a new player joins would be logical, but I'm not quite sure what the best implementation would be for this.

Thanks!

Comments

  • Hi @NicoS,

    as you have already noted, buffered RPC can get a bit redundant, especially if you only switch between an enabled and disabled state. For this scenario you maybe want to use a custom OnPhotonSerializeView solution, where you can synchronize the object's position and rotation as well as the state of it's colliders.

    Please note that you would have to transfer the Ownership of the object, if a client wants to interact with an object which is controlled by another client. To see how Ownership Transfer works, I would recommend you taking a look either at the documentation page here or at the demo which is included in the PUN package.

    If you have further questions, please feel free to ask.
  • NicoS
    Options
    Hi @Christian_Simon ,

    Your response was very helpful and helped me understand a little more about Photon. I did run into a scenario that I haven't been able to solve yet:

    Players in my application can enable/disable objects. Those objects handle syncing through OnPhotonSerializeView and their observe option is set to Unreliable on Change since I don't need to update it constantly. However, when an object is disabled and a new player joins, that player can still see the object (per default it is active).

    I'm guessing OnPhotonSerializeView is only called on active objects, which causes the newly joined player to not be in sync with the rest? Aside from disabling all other scripts, colliders and children on the object instead (which doesn't seem like a clean approach), I haven't really found a way to handle this logically. Perhaps I'm overseeing something simple?
  • However, when an object is disabled and a new player joins, that player can still see the object (per default it is active).


    As long as the GameObject is disabled, OnPhotonSerializeView won't get called on this object, which means that neither of the synchronization options would work. Other callbacks (e.g. OnJoinedRoom) won't work as well.

    You already mentioned one option: disabling all components, that are not necessary, but don't disable the GameObject itself.

    Another option would be to add some kind of an Object Manager, which handles the synchronization of those objects (most likely the enabled state).