- Home /
Order of Execution in Multiplayer
Hello
I'm currently working on a multiplayer game where I have some players and a ball I want to give randomly to a player. Now since the allocation needs to be random, the server has to chose the player and give it to each client. I do this in OnStartServer() but I only get one player when I search for my player objects with FindGameObjectsWithTag("Player"). The player objects are auto generated for each player by the NetworkLobbyManager. The scene loads fine by the way, with all players and not only one. That's why I think the OnStartServer() I call on the ball object (which is already in the scene unlike the player objects) is executed before all player objects are loaded.
Can somebody please explain me in what order all the objects/the functions (OnStartServer, etc.) are loaded/called?
Okay, second problem: When the server decides which player should have the ball (which is atm always the same because of the problem above), it saves it in a SyncVar with hook on a function that sets the parent for the ball. But this function is not called on clients (that are not hosts). When i give back the SyncVar in Update() it has a value (initially it is null). So why does the hook not trigger and execute the function as desired when it changes its value? Kinda confused by this one. EDIT: When i change the SyncVar after the game started (when I pass the ball to another player) the hook works perfectly fine. Maybe the client ball objects are not loaded yet when the server ball object changes the SyncVar value which results in the hook function not being executed on the client objects? Any idea how I can handle this?
Another problem I have is that I want to reference a GameController object to the player object. I assign it in the OnClientStart() and OnServerStart() with FindGameObjectWithTag("GameController"). This works when I host the game in the editor but throws "Object reference not set to an instance of an object" when I host the game with the standalone version. Does anybody have an idea what could be the cause of this problem?
As you see I'm slightly confused by when and how gameObjects and players get loaded on the server and the clients and how I should assign object references at runtime.
Thanks in advance for your help : )
Sincerely, Simon
PS: Would it help if I'd add the relevant snippets from my code?
Answer by toddisarockstar · Sep 15, 2016 at 08:33 PM
hmmmm. first of all, if you are using unity's master server there is nothing about gameplay "stored" on the server.
the only thing unity's master server does is give the players the other machines IP / port information so they can send data directly.
so with that in mind, if you want something random to happen for everyone, the random occurrence would have to happen in the code of only ONE of the players machines and then manually sent out to all the other players with an RPC call so everyone knows what random number was picked.
its very important in your online connection menu to make sure players scripts to know what player number they are and what belongs to who in your scene.
Nothing is automatic in multiplayer, anything updated must be sent out via RPC call.
generally a random selection like that would be done by the first player. usually called the "Host".
Answer by Lynxxx · Sep 16, 2016 at 11:40 AM
I am using UNet. If I'm right the Unity Master Server is deprecated. And in UNet the server also has the scene loaded right? (Reference)
To solve all my problems at once, I was thinking about creating a function that checks if all elements are loaded. If this is true it would send a message to the server (via a command function) that the player has finished loading. On the server I'd start a coroutine that would trigger all the above described tasks (assign ball to a random player, load reference objects) on all the clients via an RPC call. I am aware that this is SUPER complicated and I really hope anyone can give me a better solution and a better overall understanding on how the UNet order of execution and instantiation works.
Sincerely, Simon
I looked at your reference and it does look like unity has added some new automated features to the new networking system which i haven't used yet. it's calling the host a server, which confuses me cause its impossible to create a network connection without a server on a static ip and listening port. So I assume they just decided to word it that way for simplicity to new developers. Anyways, it just seems like they are attempting to do a lot of stuff for you in the new system.
I think you have the right idea!, RPC calls are your way of manually sending data yourself. so sounds like you will need to send your own confirmation system to start one task before he next one starts. if you want an Online game, the coding gets "SUPER complicated". in comparison to single player. and you will notice that because of the complexity you won't get many people here that can answer multiplayer questions!
Welcome to the world of multiplayer!
Your answer
Follow this Question
Related Questions
Spawn players in unique places (Photon networking) Object sync problem for entering players. 0 Answers
Calling the Experts! Problems with Networking: Client/Server Architecture 1 Answer
Attach Canvas from Hierarchy to MP Player Spawn 0 Answers
StandAlone Server with SQL database for all clients 1 Answer
Rigidbody that two players can move/interact with over network? 1 Answer