Search Unity

passing variable int from one script to another server/client

Discussion in 'Multiplayer' started by xeonheart, Sep 2, 2021.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    So I am having a little trouble with Syncvar between client and client/server/host, which I sorta got it working now, but not sure if this is best on how I did it, which was to use a static variable and a method to update the local variable on another script. so here I have 2 scripts, 1 is called Game Config, and if the host selects how many players they want in the game, that's how many display open positions will be posted in the lobby, or how i want it to be anyways:

    Gameconfig C# script:

    Code (CSharp):
    1.     public static int playerCount = 1;
    2.  
    3.     [SyncVar]
    4.     public int playercount;
    5.  
    6.     public void GetPlayerCount(int num)
    7.     {
    8.         playerCount = num;
    9.         playercount = num;
    10.     }
    as you can see, i am trying to syncvar the variable "playercount" all lowercase, just testing, i will clean this up and rename properly... and a STATIC variable playerCount, then the other script, which handles the lobby UI/population called NetworkRoomPlayerLobby:

    Code (CSharp):
    1.     public GameObject[] playerLobbyList = new GameObject[5];
    2.     [SyncVar]
    3.     public int maxPlayers = 1;
    4.  
    5.     private void GetMaxPlayers(int num)
    6.     {
    7.         maxPlayers = GameConfig.playerCount;
    8.     }
    9.  
    10.       public override void OnStartClient()
    11.     {
    12.         Room.RoomPlayers.Add(this);
    13.  
    14.         GetMaxPlayers(GameConfig.playerCount);
    15.         //CmdMaxPlayers(GameConfig.playerCount);
    16.         //Debug.Log("Local MaxPlayer: " + maxPlayers.ToString());
    17.  
    18.         UpdateDisplay();
    19.  
    20.         if (!isLeader)
    21.         {
    22.             startGameButton.gameObject.SetActive(false);
    23.         }
    24.     }
    25.  
    26.  
    I am not sure if this is the right approach to sync variables between a Server/Host and other clients... for the purpose of JUST getting maxPlayers to be displayed... I ONLY want the clients to read from GameConfig... but later on, i do want the NetworkRoomPlayerLobby script to write to a variable on the Gameconfig... am I doing this write, or do you recommend any changes? Also I see I can maybe use a ClientRPC, do I write the Client RPC on the NetworkRoomPlayerLobby or the GameConfig script to change the variable that will be on the GameConfig so all clients can read/write to the data?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The disadvantage with ClientRPC is any clients which connect after the RPC is sent, will need the host to send it again or they won't have the information. SyncVars are useful in that as long as the network object is spawned, all clients should maintain an up to date value for the variable, even if they join after the variable was changed.

    SyncVars though are just 1 way, only from host to client. A client changing the value of a SyncVar doesn't propagate that change back to the host. You'd use an RPC (Command) to tell the host to change the SyncVar.

    I'm assuming this is Unet/Mirror you're using.

    I don't have any experience with any premade lobby system, so I don't know if there are any different implications there. Good luck
     
  3. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    I gotya, sorry additional information, i was using DapperDino example but doing some modifications to it to add into my project:

    https://www.youtube.com/playlist?list=PLS6sInD7ThM1aUDj8lZrF4b4lpvejB2uB

    so ya I am using Mirror for this case. and just trying to follow along while adding some Minor modifications and for the sake of experiment and seeing "what if i do this, or what if i want to do that" type of add-on.

    as for me using static, is that something I am on the right track of doing, especially passing it into a function that makes it equal to a syncVar on the other script (NetworkRoomPlayerLobby)?

    i do see other tutorials on mirror, but not sure if you would recommend any of the ones listed on Mirror docs:

    https://mirror-networking.gitbook.io/docs/community-guides/video-tutorials