RPC with PhotonTargets.Others does not reach others.
Options
Hello.
I've recently decided to convert my project from Unity's Networking to Photon. Now I am in the process of reworking everything. For the most part I can see that Photon has a simpler way of getting things done (in comparison to UNET), but for the love of me I cannot get RPC related stuff to work.
In my scenario, what I was looking for is a way for one player to tell another player that he has been pushed. I've tried to send the rpc call directly to that player using
and then later
but this never reaches the desired player. I tried using PhotonPlayer.Find() with the ownerId, but it never reached either.
So instead I'm working around this by using
and
and in the RPC
I've recently decided to convert my project from Unity's Networking to Photon. Now I am in the process of reworking everything. For the most part I can see that Photon has a simpler way of getting things done (in comparison to UNET), but for the love of me I cannot get RPC related stuff to work.
In my scenario, what I was looking for is a way for one player to tell another player that he has been pushed. I've tried to send the rpc call directly to that player using
var photonOwner = obj.gameObject.GetComponent<PhotonView>().owner;
and then later
photonView.RPC("GetPushed", photonOwner, force);
but this never reaches the desired player. I tried using PhotonPlayer.Find() with the ownerId, but it never reached either.
So instead I'm working around this by using
int photonID = obj.gameObject.GetComponent<PhotonView>().ownerId;
and
photonView.RPC("GetPushed", PhotonTargets.Others, force, photonID);
and in the RPC
[PunRPC] public void GetPushed(Vector2 force, int photonID) { if (photonView.isMine) { Debug.LogError("My ID is " + this.photonView.ownerId + " and target id is " + photonID); if (this.photonView.ownerId == photonID) { Debug.LogError("IDs are equal and I am pushed with force: " + force); pushedForce = force; pushed = true; } } }The first Debug of this RPC is never called on any of the players. And if I use PhotonTargets.All, it is called - but only on the same player that calls it. I am probably doing a fundamental mistake in this somewhere and I would appreciate if someone could help me understand RPC of Photon. I am eager to continue my work with it.
0
Best Answer
-
Okay, I solved it. Turns out I was calling GetPushed on the player that was doing the pushing. I'm not sure if I tried to do it the UNET way or just got lost in the process, but anyways for future generations I leave this snippet:
Collider2D[] punching = Physics2D.OverlapAreaAll(rectOrigin, rectEnd, collisionMask); foreach (Collider2D obj in punching) { int dir; dir = right ? 1 : -1; var force = new Vector2(pushForce.x * dir, pushForce.y); obj.gameObject.GetComponent<PhotonView>().RPC("GetPushed", PhotonTargets.All, force);
Oh yeah, and for anyone that's migrating from Unet: you don't have to search for players like you would with [Command], instead you just call stuff right on them.5
Answers
-
Okay, I solved it. Turns out I was calling GetPushed on the player that was doing the pushing. I'm not sure if I tried to do it the UNET way or just got lost in the process, but anyways for future generations I leave this snippet:
Collider2D[] punching = Physics2D.OverlapAreaAll(rectOrigin, rectEnd, collisionMask); foreach (Collider2D obj in punching) { int dir; dir = right ? 1 : -1; var force = new Vector2(pushForce.x * dir, pushForce.y); obj.gameObject.GetComponent<PhotonView>().RPC("GetPushed", PhotonTargets.All, force);
Oh yeah, and for anyone that's migrating from Unet: you don't have to search for players like you would with [Command], instead you just call stuff right on them.5 -
holy shit. finally solve mine as well XD i didnt know you can target on specific photon view. tnx!0