Having some issues with RPC

Hey guys,

Firstly, i'm quite new to server programming within Unity using Photon, so forgive me if this is a completely newbie post.

I'm building a basic online game, with arena based combat. The movement and state variables of each player are managed via the stream, however things like weapon hits causing damage etc are to be done via RPC.

So i'm working on my first RPC just now. To try and keep this short, the client uses a Raycast to detect the weapon hit, and calls a function locally so that the local enemy gets damaged, and then sends an RPC to the enemy so that he causes damage to himself. (I hope I am doing this right?)

The problem I am having is a little difficult to explain so i'll break it up into stages:

1) Locally, I hit the enemy with my weapon and call the local function causing damage to the local version of the enemy.
2) I then use the hitInfo of the Raycast to find the photonView of the enemy that was hit, and use the photonView.owner as the PlayerTarget to send the RPC to.
3) here's the code showing the two above steps:
[code2=csharp]//get the pdr and cause damage locally
panelDamageReciever pdr = rayhitinfo.collider.GetComponent<panelDamageReciever>();

//cause damage server side als
controlInterface.photonview.RPC("takePanelDamage", pdr.photonview.owner, controlInterface.hammerDamageMax, rayhitinfo.collider.gameObject.name);[/code2]

rayhitinfo is the out from the Raycast.
controlInterface is where my local photonview has a public reference.
pdr (the panelDamageReciever of the enemy) also has a public photonview reference
4) I ran a test, displaying whether the enemy has recieved the RPC on his local client, and he did. So I can confirm that it would appear that the RPC went to the enemy.
5) However, although the RPC is intented to cause damage to the enemy after they recieve the RPC, the damage is actually caused to the person that dealt the damage, on the enemies local client. So to confirm, the enemy recieves the RPC, but the attacker recieves the damage.

Note, this only happens on the enemies local client. On the attackers local client, he remains undamaged, and the enemy remains damaged from the damage that was dealt locally.
6)Here's the code for the RPC:
void takePanelDamage(float damage, string panelHit, PhotonMessageInfo info)
print ("running rpc");
//get the panel
foreach(Panel panel in ci.Panels)
if(panel.name == panelHit)
print ("found panel, causing damage");
panelDamageReciever pdr = panel.panelObject.GetComponent<panelDamageReciever>();

ci in this case is the controlInterface of the person that recieved the RPC, and ci.Panel is a list of panels that the attacker could have hit.

7) I did a basic print(photonView.owner) of the person that I am sending the RPC to, and it appears blank in the console as if it is empty. I have no idea if this has any rellevance.

If there is anything more that I can provide in order to try and help resolve this please do let me know. Otherwise, any help would be greatly appreciated.


  • Hmm, I think I may have solved it.

    As you can see from the first extract of code above, where I am sending the RPC:

    //cause damage server side als
    controlInterface.photonview.RPC("takePanelDamage", pdr.photonview.owner, controlInterface.hammerDamageMax, rayhitinfo.collider.gameObject.name);

    I am sending the RPC using my own photonView. I had the assumtion that the RPC would be sent to the pdr.photonview.owner (which was the enemy's photonview), however this seems to be incorrect.

    Now that I am sending the RPC using the enemies photonview, he is the one that takes the damage.

    Is this correct though, or have a I found a very odd work-around. It seems odd that when I sent the RPC from my photonview, with the enemy as the target, I was the one that called the RPC...?

    Any help would still be appreciated.
  • Tobias
    It's correct: RPCs always target the same PhotonView they get called on. So on all clients, the method is called on the same object (or better: the objects that resemble the same entity in the networked game).
  • Thanks for the clarification, i really appreciated it.
  • Tobias
    You're welcome :)