RPC Question

I am making a multiplayer game and I am a bit confused on how exactly the RPC function works.

For instance if I call an RPC that changes the gameobject name to “test” in RPCMode.All and each player has the script, will it change only for his model in every client or will it change everyone’s name to “test”.

What if I do the same thing but I call it from the server? For example:

void ChageName()
{
 networkView.RPC("ServerChangeName", RPCMode.Server);
}

[RPC]
void ServerChangeName()
{
 networkView.RPC("ClientChangeName", RPCMode.All);
}

[RPC]
void ClientChangeName()
{
 gameobject.name = "test";
}

If there is any specified document about this I would like a link.

Well, this looks a bit… overcomplicated.

I did it by simply changing the name on the server end, then sending the RPC to other players to change the instance name on their ends, like this:

[RPC]
void SendName (name : String)
{
yourNameVariable = name;
}

void Update ()
{
networkView.RPC("nameChange", RPCMode.All); //you can also use others here, if there are issues with server doing it wrong.
}

There you go. just some help i can give about RPCs. also hope I understood you correctly.

-FuzzyQuills

I am not sure that calling an RPC from update is a good idea.

That there was just an example. you can use an RPC call anywhere you want. for something like a player being defeated, you would use an “if” statement or switch to decide when to tell the opponents a player has died. Kinda like this: (again, typing out of my head)

void Update () {
if (player.defeated) {
<do something>
networkView.RPC(<do something>, RPCMode.All, <parameters for do something>);
}
}

In your case, just have a Boolean variable in place for every time you change your name. that way, by clicking a button, the bool will come on, the RPC will be sent, then the bool could be set top false to prevent sending it again. Here’s another example: (if i got the syntax wrong, it’s because i am not used to C#!)

bool sending;
String typer;
[RPC]
void sendName (String info){
name = info;
}
void Update () {
if (sending){
name = typer;
networkView.RPC(sendName, RPCMode.Others, typer);
sending = false;
}
}

void OnGUI () {
if (GUI.Button(Rect(<button dimensions>), "send name")){
sending = true;
}
typer = GUI.TextField(Rect(<dimensions>), typer);
}

the typer variable could then be tied to a text field in OnGUI, to change the name easily. you can also tie a button to the bool variable in the same way.

Good luck!

-FuzzyQuills

EDIT: just changed the code to state the OnGUI function