I found out that clients that reconnect to the game load the host’s scene automatically when connecting to the network (I’m using the sample Netcode Bootstrap). I would like to know where in the code they are loading the host’s scene because I want to make sure they receive some data before they do so.
Well, I found out the bootstrap sample wasn’t provided by Unity (sorry for the confusion). But I think it’s got nothing to do with it.
Upon checking the documentation, I realized that as soon as the client has the green light (when the connection is approved at the “NetworkManager.Singleton.ConnectionApprovalCallback”), the client automatically loads the host’s scene before the “OnClientConnected” callback gets called. This is supposed to be done internally and automatically, so I have no clue how to perform some actions before they load the scene (namely sending the server’s info to the client via “ClientRpc”, which can’t be done since the player connecting is not yet a client: it first loads the scene, then the player becomes a client).
I discarted the network variables since I have to update and then send a lot of variables just in a specific moment. But regardless of using a struct and sending it over an RPC call or using network variables, I still face the same problem: the client updates when he connects, which is after the host’s scene is loaded (which is too late because I want the updated parameters to have effect on some game object’s Awake and Start methods from the host’s scene).
So the only way that occurred to me was to manually load the host’s scene after the player is connected and have receive all the updated info.
Let me know if I didn’t explain myself correctly. Also, if you have an alternative in mind, that’ll be very helpful too.
Thank you for pointing me to this thread! I feel this could be the answer to my problem
Could you please elaborate a little bit on
?
I tried using an RPC after the connection is approved but it seems too early. Will the RPC call work when the SceneEventType.Synchronize callback gets called?
PS: In case it’s relevant to any answer, this is the way I update the client’s info:
(1) upon connecting, a Server RPC is called to update a struct containing all the useful parameters with the server’s parameters’ values.
(2) Then a targeted Client RPC is called to send the struct and update the client’s parameters’ values.
The system works perfectly but it arrives too late in case I call it on the “OnClientConnected” callback, as I mentioned. If called after the “connection approval” callback, then the Client RPC call doesn’t work (since the client is not yet connected).
You’d need to use a custom message rather than a RPC, this sends a message between network managers without needing a network object.
It will be NetworkManager.Singleton.SceneManager.OnSynchronize you’ll want to subscribe to. I only did a quick test with it so maybe @CosmoM can chime in and let us know if it worked for him, otherwise I can dig out the test I did but it shouldn’t be too hard to set up a new one.
I was finishing other parts of the project before delving properly into custom messages, so I couldn’t report any solid findings. However, I was using @cerestorm 's suggestion about using “NetworkManager.Singleton.SceneManager.OnSynchronize”, and it works perfectly: the callback is called when the client is already connected but before the scene is fully loaded, which allows you to send the data before the Start() method from the host’s scene is called (it specifically happens in between the host’s scene’s Awake() / OnEnable() methods and the Start() method).
However, I have no idea how to stall or avoid loading the host scene automatically for you to be able to load a custom loading screen.
Thanks for the answer. I tried custom messages myself - it’s a dead end, Netcode doesn’t accept it on a client with an error “Trying to receive NamedMessage from client 0 which is not in a connected state.”
I try to send this message during the NetworkManager.Singleton.ConnectionApprovalCallback delegate, maybe I do something wrong.