Using this setup, EACH player will have its own chatty NetworkView sending out information to everyone else more or less continuously. We're not going to take the standard route. However, we're still going to need a single NetworkView because that's the only way Unity can communicate over the network (since we don't want to re-create the wheel and build our own Socket manager in. However, a single NetworkView component attached to an empty GameObject can be set to State Synchronization=OFF, and Observed=NONE. This will allow us to send RPCs back and forth between client and server without all the overhead and without all the unnecessary complexity. Which would leave us with something like this:Īt the same time, we still want a way to guarantee a unique ID is assigned to each player. NET's functions specifically designed for this task. But if we did that, then we'd have to import at least another package - which isn't really worth it just for one function. Devising a way for clients to be able to distinguish between messages that are meant for them and messages that are meant for other players.Managing the creating and destruction of player objects on all clients (from the server) as players join or disconnect.Maintaining a dictionary between GameObjects and NetworkPlayer objects so that we can easily lookup one from the other.Having just a single NetworkView over which RPCs can be sent means there are a few extra responsibilities we have to take on: A better option is to just use AllocateViewID() to get a unique object on the server even though our actual player object won't have their own unique network views. To help explain how this is accomplished, let's take a look at a process flow diagram. Did I mention that I <3 process flow diagrams. I should probably mention that before we get too far into things and PFDs start flying all over the place. OK, let's look at the important parts, starting with our first RPC in OnPlayerConnected() networkView.RPC("JoinPlayer", RPCMode.All, newViewID, Vector3.zero, p) If you haven't done so already, grab the Unity package and open up the networkController script so you can follow along. In OnPlayerConnected(), we execute this RPC call to all connected clients using the server's world networkView. Notice that we are using RPCMode.All - which will send the JoinPlayer() RPC to all players and the server. Now, in the JoinPlayer() RPC, we have this critical comparison: if(p.ipAddress=LocalAddress) which is removed when a player disconnects with something very similar in the DisconnectPlayer() RPC: players.Remove(player) This is done in the JoinPlayer() RPC with this simple command: players.Add(p,newPlayer) The player object must be created on all clients and the server, and this is the simplest way of accomplishing that.Īlso, notice that the server AND the client is maintaining the HashTable of all players. In Start(), we determine the computer's Local IP Address using Dns.GetHostEntry(Dns.GetHostName()) from. NET's package and then use this to compare it against the NetworkPlayer structure's "ipAddress" attribute that is being sent from the server.
0 Comments
Leave a Reply. |