- Home /
Best way to access player's gameobject across network
So I'm trying to make a simple player customizer in a multiplayer game.
Currently I have:
Clicked button sends choice to "local" player
Player RPC's network manager (so network knows who wants to switch)
Manager RPC's the change to everyone
I can't figure out how to keep track of - and reference - the players correctly though. For example, NetworkViewID doesn't seem to be convertible to something useful (like the gameobject it's attached to). I was thinking of putting the players in an array but I don't know how to implement it on a network correctly.
What am I missing?
Any help appreciated
Answer by PatriceVignola · May 10, 2015 at 10:24 PM
You need to assign a unique ID to each of your player, something like an index starting at 0 for the first player. On all clients, keep an array of all players (it must be the exact same array). Then, when the player clicks on the button, RPC the action information to all players (or all other players) by sending the playerID:
nView.RPC("ExecuteActionOnPlayer", RPCMode.OthersBuffered, playerID);
Then, when the other players receive the RPC message, they will execute the action based on the playerID you sent them:
[RPC]
void ExecuteActionOnPlayer(int playerID)
{
Player player = playersArray[playerID];
// Execute "player" action here
}
You have to make sure that the array stays the same on all clients, but it shouldn't be too hard since players don't usually get modified every frame. I think the correct way to implement this is to build the array once on all connected players, and if you ever need to make a change to it, synchronize it via an RPC call. You can also send more than one ID if you want to execute an action that involves more than one player.
If this is not what you wanted, please give us more explanation on exactly what you want to do over the network.
So to clarify, assign each player gameobject an int, and handle everything from within the player script itself?
Yes, exactly. You can handle everything in the player script itself just fine, but later you might want to have an additional layer if you ever want your game to be player either online or offline, but that's beyond the scope of the question.
Anyways, you can assign each player gameobject a unique int, and in your Game$$anonymous$$anager or somewhere appropriate, you store the array of players that's going to be the same for everyone.
Answer by toddisarockstar · May 10, 2015 at 09:49 PM
with my last multiplayer projects, I simply have the host assign the other connecting machines a simple player number in a RPC call soon as they each connect.
once you have that, everything else is simple.
when a player makes his selection, I manually include the player number somewhere in the RPC call. this makes it easy for all the connected machines to keep track of who is doing what.
then when you get to making your Arrays of player selections, obviosly the player number would be the same as the arrays index number representing the player. so it makes it very easy to assign incomming info directly into your arrays.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Convert Network.ViewID to Int 1 Answer
UNET Server not setting [SyncVar] 0 Answers
Brute Force Search Algorithm 0 Answers