- Home /
Sending an RPC back to its original sender
I'm trying to send an RPC back to the original sender like lets take for example. player1 sends an RPC to player2 telling player2 to loose health. How do I make player2 identify who the RPC came from and send it back to player1?
I've already tried getting the networkView of player1 and sending it along with the first RPC but what I ended up doing was
hit.RPC("loosehp",RPCMode.All, sender.networkView);
@RPC
looshp(NetworkView sender)
{
sender.RPC("addkill",RPCMode.All);
}
(This is not my exact code it was just something I wrote to give a general Idea on how I did it) but I get a parameter error. What am I doing wrong?
Answer by senad · Feb 03, 2012 at 09:19 AM
To do this, you need to define the NetworkMessageInfo parameter in your first RPC method like here: http://unity3d.com/support/documentation/Components/net-RPCDetails.html
Then inside this RPC method you get the sender object from the NetworkMessageInfo and use it to call a RPC-method on the original sender. For this you use the second type of RPC call described here (the one that uses NetworkPlayer as a target): http://unity3d.com/support/documentation/ScriptReference/NetworkView.RPC.html
I hope it works. :)
Thanks but I don't think you are getting what I mean. What it does is it finds who the raycast hits and sends the RPC to them. How would I make it detect who shot them and send an RPC to the person who shot them? Basically what I'm asking is how would I turn NetworkViewID into NetworkView?
I am afraid I still do not get it. By person you mean the NetworkPlayer? Are you trying to find the originating player or the game object?
originating player. I'm trying to send the players networkview along with the rpc that is being sent to the other player so I can resend an rpc to the original player.
In that case have a look at the first of the two links. Every RPC calls automatically sends a Network$$anonymous$$essageInfo object. In there you can find the originating player for the message call. You should be able to figure it out from the manual.
I will try to post you some code later. (when i finish work)
:)
This is C# syntax, but should also work in javascript.
[RPC]
looshp( Network$$anonymous$$essageInfo messageInfo )
{
networkView.RPC( "addkill", messageInfo.sender, null );
}
On the receiving side of addkill(), you will be at the player who originated the first RPC call. Here you can add the kill to the global stats or whatever you like to happen.
The messageInfo object will get sent automatically by the engine. No need to do it manually.
Answer by syclamoth · Feb 07, 2012 at 11:11 AM
You can't send networkView objects via RPC calls. Instead, use this:
hit.RPC("LoseHP", RPCMode.All, sender.networkView.viewID);
[RPC] LoseHP(NetworkViewID id) { NetworkView view = NetworkView.Find(id); }
this will allow you to refer to specific networkViews remotely, without sending the actual object itself.
So why do you not use the Network$$anonymous$$essageInfo object to send back an RPC to the originating NetworkPlayer only?
I think the whole point is to increase a kill counter at the originating player. So having the network view id, we still do not know which player to send it to specifically.
Yes?
Yes, you can do that. The point still stands, you can't send NetworkViews directly over RPCs.
Answer by Bunny83 · Feb 04, 2012 at 10:29 AM
Your code looks like it's a mixture of C# and UnityScript.
I guess you want to use UnityScript (Unity's Javascript):
hit.RPC("loosehp",RPCMode.All, sender.networkView);
@RPC
function looshp(sender : NetworkView)
{
sender.RPC("addkill",RPCMode.All);
}
or if you want to use C# it should look like this:
hit.RPC("loosehp",RPCMode.All, sender.networkView);
[RPC]
void looshp(NetworkView sender)
{
sender.RPC("addkill",RPCMode.All);
}
I've already tried that and It gave me a parameter error.
Answer by Pangamini · Mar 09, 2012 at 05:44 PM
Is it possible to send rpc to everyone except the original sender? Like the sender client wants to broadcast a chat message, sends it to server and then server sends the message to every other client?
Your answer
![](https://koobas.hobune.stream/wayback/20220613063707im_/https://answers.unity.com/themes/thub/images/avi.jpg)