Variable exchange

I've been looking for a way to exchange variables between players in a very specific way (which I thought would be easy but I can't seem to find the answer). My game is a dice game, all I want is to send my dice value to the other player and receive their dice values and exchange hp. I can't find how to do so.
I'd be forever grateful if anyone can help me!

Comments

  • matthewjd24
    edited July 2020
    Hi shujinghost, you might be thinking of an RPC or RaiseEvent. These are the lifeblood of a multiplayer game since it allows you to send a message or whatever data you want to the other players in the room. You can check out this tutorial:

    https://doc.photonengine.com/en-us/pun/v2/gameplay/rpcsandraiseevent

    If you use an RPC, it calls a method on that GameObject in all players' clients, and data can be sent in the RPC for the clients to use. You can use an object array to make this easy to send different types of variables in one message. RaiseEvent, I believe, isn't associated with any GameObject in particular. I haven't used it too much though, I've just been sticking to RPCs ;)

    So in your game, you could create a script that acts a tracker for your dice values and your opponent's. When you roll your dice, it records it in the tracker, and then the tracker sends off an RPC to the other player in the room and lets him know the values you just rolled. To call the RPC you would use this:
    object[] diceRolls = new object[2];
    diceRolls[0] = firstDiceValue;
    diceRolls[1] = secondDiceValue;
    photonView.RPC("DiceRolled", RpcTarget.Others, diceRolls);
    

    The code in the RPC might look like this
    [PunRPC]
    void DiceRolled(object[] diceValues)
    {
        Debug.Log("his first dice value was " + (int)diceValues[0]);
        Debug.Log("his second dice value was " + (int)diceValues[1]);
    }
    

    At least that's how I would go about it. I'm not an expert by any means.