How to handle object instantiation for newly joined players?
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
-
So...
Turns out this was due to a silly oversight. To test this functionality I set up a simple logic that goes:
- Connect to master
- Join or create room
- If you're the master client: load in the desired level
- Otherwise the desired level is loaded through
AutomaticallySyncScene
All good and well, except...
3.
If you're the master client:load in the desired levelI 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 throughAutomaticallySyncScene
, and once more throughSceneManager.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 😥.
1
Answers
-
So...
Turns out this was due to a silly oversight. To test this functionality I set up a simple logic that goes:
- Connect to master
- Join or create room
- If you're the master client: load in the desired level
- Otherwise the desired level is loaded through
AutomaticallySyncScene
All good and well, except...
3.
If you're the master client:load in the desired levelI 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 throughAutomaticallySyncScene
, and once more throughSceneManager.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 😥.
1