How to handle object instantiation for newly joined players?

Options

Hi,

We are currently working on a multiplayer game, and we want players to be able to join a match that has already started. Currently we are trying to achieve this by setting PhotonNetwork.AutomaticallySyncScene to true, so if a level is already loaded in when joining the player automatically loads that level and all the instantiated objects in it.

However, it appears that any buffered instantiate calls are executed before the scene is finished loading, meaning that once the scene fully loads, those objects instantly get destroyed because the level is changed. As a result, newly joined players do not see any previously instantiated players or objects.

Is there any way to make this work without resorting to a custom solution that can account for scene loads that happen on join?

Best Answer

  • kiam3dat
    kiam3dat
    Answer ✓
    Options

    So...

    Turns out this was due to a silly oversight. To test this functionality I set up a simple logic that goes:

    1. Connect to master
    2. Join or create room
    3. If you're the master client: load in the desired level
    4. Otherwise the desired level is loaded through AutomaticallySyncScene

    All good and well, except...

    3. If you're the master client: load in the desired level

    I forgot to put in a check to make sure only the master client calls SceneManager.LoadScene, causing the newly joined client to load the scene twice (once through AutomaticallySyncScene, and once more through SceneManager.LoadScene). This double scene load caused all the newly instantiated objects to immediately be destroyed along with the scene.

    Well, at least I've solved the problem... just wish it hadn't taken me 4+ hours to figure out 😥.

Answers

  • kiam3dat
    kiam3dat
    Answer ✓
    Options

    So...

    Turns out this was due to a silly oversight. To test this functionality I set up a simple logic that goes:

    1. Connect to master
    2. Join or create room
    3. If you're the master client: load in the desired level
    4. Otherwise the desired level is loaded through AutomaticallySyncScene

    All good and well, except...

    3. If you're the master client: load in the desired level

    I forgot to put in a check to make sure only the master client calls SceneManager.LoadScene, causing the newly joined client to load the scene twice (once through AutomaticallySyncScene, and once more through SceneManager.LoadScene). This double scene load caused all the newly instantiated objects to immediately be destroyed along with the scene.

    Well, at least I've solved the problem... just wish it hadn't taken me 4+ hours to figure out 😥.