RPC problem

marfmarf
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

  • Is the client where it does not change the local one which sends the RPC?
  • 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?
  • 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).
  • 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?
  • 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?
  • 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?
  • 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
  • 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
  • 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?!
  • Ok, this afternoon I will try to re-upload all with changes.
Sign In or Register to comment.