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

Static variables and [SyncVar]

Discussion in 'Multiplayer' started by MornFall, Jun 19, 2015.

  1. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160
    Hello everyone.

    So i made a little top down RTS / Tower defense. The player build turrets and units.
    The way i made the code : i am using a LevelManager.cs that handles all that.
    it contains the list of the units ( objectpool ), manage the Queues for the unit spawning ( units spawn at fixed position , or get in a spawnqueue if not spawn poisition is currently available. It also contains all the team data ( Energy, ressources, ect... )
    All that... via static variable and static function...
    So my thought was : if i do it like that, all i will have to do will be to have this big level manager running on the server, and the clients will simply send their commands to it. the manager will do his thing and will send to the client which gameobject to activate... mostly.

    That's for the context... now the issue :
    I got the players in... all good. But i just found out that running the levelmanager on"serveronly" , the data do not get pulled from it by clients. Like : Team1 is supposed to have the "levelManager.current_Team1_Energy" displaying the UI... but it is currently not...
    What am i missing ?

    I tried something th.. removed the "Server only" on the levelmanager.networkIdentity and put [SyncVar] in front of my T01energy amount ... It now gives me an error " Syncvar can't be used for static variable" ...

    Did i simply miss something, or do i have to reprogram the entire game to make it 1 v 1 ready ?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Right, SyncVars on static variables is not supported. It is something that has been talked about, but it is difficult as the concept doesn't work well with spawning. If MyClass.myVar is accessed, it might be before an object was spawned, or even before connecting to the server, so it's value may be completely different from the server's value.

    You could make a singleton for your levelManager object and make the variables non-static.
     
  3. MornFall

    MornFall

    Joined:
    Jan 11, 2013
    Posts:
    160
    I understand. I will modify how i handle all the ressources.
    Something is still bugging me though...
    The way my game is : units / turrets do not need to be synced. The real only thing they need to be is "activated / deactivated" at the same time on all clients. That is all.

    Could you please tell me if my logic is wrong :
    I want to have the LevelManager on "ServerOnly".
    1 : the player only sends the command to put unitX in the queue on levelManager.
    2 :the level manager does his thing, remove the ressources and all that, and dispatch o the clients which gameobject to activate.

    Is it wrong to try to go that way , and if not, should i go with "RPC Calls" ? ( i have yet to find a clear example for that on the documentation... )

    Thanks @seanr .