RPC problem

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

RPC problem

marf
2012-02-12 19:50:26

Hi, I have a function that if I hit a player call an RPC that change the life of the hit player over the network, this is my code:

int damage = Random.Range(5, 15);

Debug.Log("Calling setlife with value: " + (hit.collider.transform.parent.gameObject.GetComponent().life - damage));

hit.collider.transform.parent.gameObject.GetComponent().RPC("SetLife", PhotonTargets.AllBuffered, (hit.collider.transform.parent.gameObject.GetComponent().life - damage), gameObject.transform);

And this is my RPC:

[RPC]

    void SetLife(int lifeNew, Transform killer)
    {

        if (lifeNew > 0)
        {

            gameObject.GetComponent<PlayerNetwork>().life = lifeNew;

        }

        else
        {

            Debug.Log("E' morto!");
            gameObject.GetComponent<PlayerNetwork>().life = 0;
            gameObject.GetComponent<PhotonView>().RPC("RespawnPlayer", PhotonTargets.AllBuffered, killer);


        }

    }

If I hit a player in my local player editor I see that the other player's life is changed, but in the other game window (the one where is playing the player who was hit), his life doesn't change.

Why?

Maybe the problems happens because my PhotonView observe the Player Transform, I need to attach another photonView that observe the PlayerNetwork script (the script of life)?

Comments

dreamora
2012-02-13 08:39:09

Is the client where it does not change the local one which sends the RPC?

marf
2012-02-13 09:09:09

Basically it doesn't change in any case, I think the problem is because I observe only the transform of the photon view in my RPC and not my PlayerNetwork script, right?

Tobias
2012-02-13 13:05:23

The RPC method must be in a script in the same PhotonView you use to call the RPC. PhotonView.RPC won't just call any fitting method.

Please don't do everything buffered. PhotonTargets.AllBuffered must be used carefully, cause each call will create a event which is sent when new players are joining. Your live value is definitely a temporary value which is best sent non-buffered and probably even "absolute" (remaining live points).

marf
2012-02-13 14:12:01

Ok, in my case I have a photon view attached to my player prefab that is observing his transform, playernetwork is the script where I call the rpc and where the rpc is situated, too. (in this script I register the life). Is this a correct solution?

marf
2012-02-14 20:31:59

Can you answer please, I've this problem since January and I can't know why it isn't working, now, if I shoot with compiled game to the editor (it can be client or server) it tells me that the rpc doesn't exist, only with the compiled game shooting the editor, why?

Tobias
2012-02-16 17:43:46

You have to call the RPC on the PhotonView you want to affect. If "playernetwork" is attached to a GameObject and knows that GO's PhotonView, it could call photonView.RPC("method",...)

The compiled version works and the one in the editor doesn't? This is really weird. Check what I said above and let us know. In worst case: can you send your project?

marf
2012-02-19 11:58:46

Yeah I do all as you said. Now I put the link to download the unity package of my forum. For login accounts put:

Username: marf Password: 12345

or

Username: lupus Password: 12345

The bug happens when you hit a player from the compiled version.

If you found errors, please tell me what's wrong.

LINK: http://dl.dropbox.com/u/31630542/wararmProject.unitypackage

marf
2012-02-20 18:05:19

News about the problem?

The only things that needs to be fixed is that in MainMenu script you need to change PhotonNetwork.PlayerName with a string var that you create.

Then you need only to add the scenes:

  1. Lobby
  2. LoadingScreen
  3. Camp
  4. Rave

Tobias
2012-02-21 17:36:00

The project export seems to be a bit broken. I see "join alpha team", etc but then it fails for more than one reason:

  • It's not prepared to work without playerprefs (i don't know which are needed and how to set them)
  • When I use Lobby as start scene, there is a nullreference in the gui and nothing happens
  • Using LoadingScreen, i see said buttons but from there nothing gets loaded nor connected
  • I have no clue where to apply/set the username(s) you said i should use
  • the MainMenu script obviously tries to connect. For some reason (here), the GameManager immediately tries to join a room, which it can't cause it's still disconnected.

Can you update the package and guide, so it's easier to get to the point of helping you with RPCs?!

marf
2012-02-22 13:23:18

Ok, this afternoon I will try to re-upload all with changes.

Back to top