- Home /
Sending an RPC
How would I go about sending an RPC back to the send of the last one? Lets say.. I sent an RPC to a health script the took away the other players health and once the health equaled 0 how would I send an RPC back to the killer telling him to add the kill? is it something like -
info.sender.RPC("addkill",RPCMode.All);
but that doesn't seem right and doesn't work.
Answer by Owen-Reynolds · Jan 23, 2012 at 05:39 PM
Networked code doesn't usually try to do things that way. Say you have 4 players and one bullet. The bullet was spawned by player 3, so it controls it and the other players mirror the position (the bullet code looks like: if not owner {do nothing}
.)
When the bullet, in player 3's copy, hits player 1, it sends out bulletHit(player1, 8 damage, RPC.ALL).
All four players run the math, see player 1 was dead, give points... .
If you have more a server/client version, the server program runs everything. It does the damage, scores ... and sends all client messages like updateScore(p3, newScore), or beginDeath(player1).
Well how would it tell who player1 is? That is what my question is.
The same way without a network. The bullet eventually triggers an OnCollision(Collision col)
. It looks up PLY=col.transform to see who it hit and do damage. That's a transform.
At some point we want the number of who was hit so we can keep stats. $$anonymous$$aybe the npcs are numbered (check PLY.GetComponent(npcScript).num) or maybe we kept a list when we made them, so we check that list for which slot.
What about raycasts? I want to get who the shot the raycast. Not who the bullet object hit.
Same basic principle. Nothing really changes other than the origin of the signals. When you send your RPC, include a reference to the NetworkPlayer who fired the shot- that way, when it arrives the target will know what to do with it. Remember that NetworkPlayer instances are one of the few types that can be sent over RPC calls.