Search Unity

RPC and Command Calls OR Network messages for syncing variables across Server and Client?

Discussion in 'Connected Games' started by jnguyen54228, Nov 7, 2018.

  1. jnguyen54228

    jnguyen54228

    Joined:
    Mar 2, 2018
    Posts:
    3
    I'm currently making a Unity Multiplayer game that relies on the synchronization of variables across the Server and the singular Client. I'm already able to use [SyncVar] to sync variables from the server with the client, but not the other way around. The thing is, I don't have a player prefab that spawns in the game. Am I still able to make Command and RPC calls? I tried this code below which didn't end up working when I played the game. Would Network messages work better for my situation?

    bool officeBought = false;

    void Update () {
    if (!isServer)
    {
    CmdSendBuildingBought(officeBought);
    }
    }

    [Command]
    public void CmdSendBuildingBought(bool buildingBought)
    {

    buildingBought = true;
    }
     
  2. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    2,971
    What do you mean with 'you don't have a player'?
    Where does the server send the data to then?
    Commands and RPCs definitely need a NetworkIdentity on a GameObject.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    4,677
    Why would you use the HLAPI without a player object? It doesn't need to be have a model in the game. You can just use it as a convenient place to store all the Commands you want to send back to the server. Then you just sync variables using SyncVars from the server to the clients, and if you need to update one from a client you send it to the server using a Command on the Player object.