MonoBehaviourPunCallbacks order of script execution

conraddu

Member
Hello,

I have a question regarding the pun integration and the order of execution for PUN callbacks. Specifically, my question is regarding two scripts: SpawnManagerBase and PunCharacter.

What guarantees that the master client will execute SpawnManagerBase.OnPlayerEnteredRoom() before PunCharacter.OnPlayerEnteredRoom()? It is critical that the master client always executes SpawnManagerBase.OnPlayerEnteredRoom() first, since the remote players need to exist on the new client before PunCharacter.OnPlayerEnteredRoom() tries to send RPCs for them to do things like equip items.

I know that these two OnPlayerEnteredRoom functions are not entirely identical, since one implements the MonoBehaviourPunCallback function and the other is executed with the event handler after the MonoBehaviourPunCallback is hit, but as far as I can tell this doesn't effect the order of execution.

The reason I bring this up is because I created a new script for spawning my AI agents, called AgentSpawnManagerBase, which is quite similar to SpawnManagerBase. I keep track of the PhotonViews (which are the agents) as they are created, and raise a new AgentInstantiation event w/ PhotonNetwork.RaiseEvent when OnPlayerEnteredRoom is called.

This only partially works for creating my agents on new clients, because I can see that PunCharacter.OnPlayerEnteredRoom() is called before AgentSpawnManagerBase.OnPlayerEnteredRoom() which results in the RPC's from PunCharacter failing because the agents do not exist on the new client yet. So you can see how the order of operations is being broken here, but it is unclear to me why the order of operations would be broken for my AgentSpawnManagerBase but not be broken for SpawnManagerBase.

Do you have any recommendations on how this might be fixed to guarantee PunCharacter.OnPlayerEnteredRoom() is alway called before AgentSpawnManagerBase.OnPlayerEnteredRoom()?
 
PunCharacter doesn't implement the MonoBehaviourPunCallbacks version of OnPlayerEnteredRoom. MonoBehaviourPunCallbacks.OnPlayerEnteredRoom has one parameter whereas the PunCharacter implements a two parameter version. This is triggered from the MonoBehaviourPunCallbacks with the event "OnPlayerEnteredRoom" called from the SpawnManagerBase. The SpawnManagerBase.OnPlayerEnteredRoom should always be called before PunCharacter.OnPlayerEnteredRoom.
 
Oooh I see, SpawnManagerBase.OnPlayerEnteredRoom() executes the EventHandler OnPlayerEnteredRoom event. That was the critical detail I was missing. So, it seems like it is incorrect for my AgentSpawnManagerBase to implement OnPlayerEnteredRoom from MonoBehaviourPunCallbacks.

What would you recommend for implementing a class like AgentSpawnManagerBase? It seems a bit complicated because I can't really extend MonobehaviourPunCallbacks and just implement OnPlayerEnteredRoom, since that breaks the script execution order. One option would be to have a more encompassing SpawnManagerBase that handles spawning both players and agents, that way we guarantee that the EventHandler's OnPlayerEnteredRoom event only gets executed once all the clients have been told to create all the characters (both players and agents) in the game. But that kind of muddies up the responsibility of the SpawnManagerBase. Any thoughts on this?
 
The purpose of calling AgentSpawnManagerBase is for AI agents? And instead of the agents being already in the scene you want to spawn them at runtime? I'm not sure the best route for this, it may take some modification of the spawn manager. I can look at this though for the next update.
 
Yup that's correct, the AgentSpawnManagerBase just acts as a means of spawning AI agents so that they don't have to be placed in the scenes manually.

I think it would be a nice addition, considering that SpawnManagerBase already exists but only for Players. Looking forward to any updates on this! I will probably tack a wack at a solution for it as well. Thank you!
 
Top