Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to sync game currency between players

Discussion in 'Multiplayer' started by pKallv, May 20, 2017.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    Trying to sync game currency between players but have problems to get it to work properly.

    Here is the code:
    Code (CSharp):
    1. [SyncVar] private int redValue = 10000;
    2.     [SyncVar] private int blueValue = 20000;
    3.     private int redFundsX = 10000;
    4.     private int blueFundsX = 20000;
    5.  
    6.  
    7.     void Update () {
    8.        
    9.         if (redValue != redFundsX || blueValue != blueFundsX) {
    10.             print ("redValue: " + redValue + " != redFundsX: " + redFundsX + " || blueValue: " + blueValue + " != blueFundsX: " + blueFundsX);
    11.  
    12.             print ("HIT");
    13.  
    14.             DoPlayer (redFundsX, blueFundsX);
    15.  
    16.             txt_RedTotalFunds.text = "$" + redValue.ToString();
    17.             txt_BlueTotalFunds.text = "$" + blueValue.ToString();
    18.  
    19.             redFundsX = redValue;
    20.             blueFundsX = blueValue;
    21.             print ("blueFundsX: " + blueFundsX + " blueValue: " + blueValue);
    22.         }
    23.  
    24.     }
    25.  
    26.  
    27.     [Client]
    28.     void DoPlayer (int _red, int _blue) {
    29.         if (hasAuthority) {
    30.             print ("Authority");
    31.             Cmd_Update (_red, _blue);
    32.         }
    33.     }
    34.  
    35.     [Command]
    36.     void Cmd_Update (int _red, int _blue) {
    37.         if (redValue != _red) {
    38.             redValue = _red;
    39.         }
    40.  
    41.         if (blueValue != _blue) {
    42.             blueValue = _blue;
    43.         }
    44.     }
    45.  
    46.     void Btn_RED_test () {
    47.  
    48.         if (!isLocalPlayer)
    49.             return;
    50.         print ("RED: " + gameObject.tag);
    51.         redFundsX = 1234;
    52.     }
    53.  
    54.     void Btn_BLUE_test () {
    55.  
    56.         if (!isLocalPlayer)
    57.             return;
    58.         print ("BLUE: " + gameObject.tag);
    59.         blueFundsX = 5678;
    60.     }
    Basically what I am trying to do is that when click one of the buttons I change redFundsX and blueFundsX, which trigger the if-statements in Update.

    It works with red and sync between the players. However, when I click on blue button I get problems.

    result from print:
    It loops between the two above and I cannot really figure out how to fix this.

    Would really appreciate help.
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You are sending a one packet to the server every client frame just do to a equality check. That's very heavy. Rather make Command methods for Cmd_AddRed, Cmd_AddBlue, Cmd_SetBlue,Cmd_SetRed, Cmd_SubtractRed etc.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    good point, thanks