RPC function: from who did it came?

Hi!

I am fairly new to network coding in Unity, and one thing that confuses me a bit is how do determin a “player ID”.

What I am trying to accomplish is to assign a unique player ID to everyone in the game. Or does Unity do this already?

When a RPC call is executed locally, how do I know from which player it came, and e.g Destroy that player?

Thank you!

You can append an optional NetworkMessageInfo to each RPC function.

@RPC
function MyRPC (info : NetworkMessageInfo)
{
print(info.sender.ToString());
}

This contains a sender variable which is the NetworkPlayer.

Aaah wonderful! Thanks.
I was confused with some terminology.

Magic stuff, this networking.
I was surprised to see that Network.Instantiate does in fact does something similair to a “buffered NPC” call.
I did instantiate some spheres when pressing the space bar, and another client who logged on later, was perfectly in sync with the level (which I did not expect).

Hmm but in your example, how do I construct the function?

RPC FUNCTION CONSTRUCTION (?)

function chatMessage(info : NetworkMessageInfo, message : String)

RPC FUNCTION CALL (?)

networkView.RPC("chatMessage",RPCMode.AllBuffered, NetworkMessageInfo, stringToEdit);

info : NetworkMessageInfo must come last in the receiver. You don’t need to put it into the networkView.RPC. It will just be used if it’s in the function or ignored if it’s not.

Wow. You’re fast :slight_smile:

Thank you. I didn’t know i wasn’t supposed to treat it like a regular parameter. Something for in the docs?

My NetworkMessageInfo.m_Sender fields always have index 0, even though I have 2 clients and a server sending to each other. Create an RPC function with NetworkMessageInfo as the last parameter.
Repro steps:

  1. compile and run a Unity app that uses Network via the C# scripting API, so that you have 1 server and 2 clients on the same machine
  2. connect the clients to the server
  3. send an RPC from NetworkPlayer 1 (RPCMode.Others). Notice that it is received with NetworkMessageInfo.m_Sender == 0
  4. send an RPC from NetworkPlayer 2 (RPCMode.Others). Notice that it is received with NetworkMessageInfo.m_Sender == 0