Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Synchronize Scripts between Server and Client

Discussion in 'Netcode for GameObjects' started by Navil, Oct 7, 2023.

  1. Navil

    Navil

    Joined:
    Nov 8, 2014
    Posts:
    7
    So I do have a basic understanding of the networking concepts of Unity.

    I however get a little bit confused of what gets replicated. My Understanding is:
    • Everything that is a NetworkBehaviour, is capable of snychronizing data cross server/client
    • NetworkVariables/NetworkLists are the go-to way to synchronize data between server/client
    Now I am building a game where the server is spawning units (which in return have abilities) and I want those to be available at the client too. For that I would apply a NetworkObject to the gameobject, add my needed scripts (NetworkBehaviour for those that "share" data, Monobehaviour for those that dont). One of those NetworkBehaviours is "Unit".

    So that the player can keep track of the cooldown of abilities or the health of the unit, all those variables would need to be NetworkVariables. The place where I struggle now tho is:
    How do I replicate the List of Units from the Server to the Client? I tried NetworkList<Unit>, but apparently I can only use structs here. Should I just replicate the NetworkIds and then get the Unit from their replicated NetworkObjects? Or should I implement this ISerializable interface, which looked very complicated with alle the values (animations, abilities, ..) a unit has.

    Help/Insight would be highly appreciated. Maybe I also just misunderstood some concepts.
     
    Last edited: Oct 7, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,583
    Replicate the list on the client side by hooking into OnNetworkSpawn, adding each Unit to the client‘s list when the units’ owner matches the local client.
     
    Last edited: Oct 9, 2023
    Navil likes this.
  3. Navil

    Navil

    Joined:
    Nov 8, 2014
    Posts:
    7
    This works thanks. As I am a bit confused: Why does this sync between server/client if it is not a NetworkVariable? Isnt this the whole purpose of a NetworkVariable?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,583
    A lot of newcomers to networking seem to be focused on actually transmitting (synchronizing) everything that needs to be in sync. However the reality is that you can often replicate the same behaviour on the client side with the information available to them (this case, every spawned object runs OnNetworkSpawn and the state can be replicated from that).

    Or where there is no synchronization necessary, for example if one player starts shooting a machine gun weapon. Commonly one might be tempted to send an RPC for every bullet fired and also making each bullet a network spawned object. However, you only need two RPC calls: start and stop shooting weapon. What kind of weapon is shooting is however already known to the client, thus the client can infer the frequency with which it spawns projectiles from that. On top the behaviour of the projectiles (eg speed, visuals) is known too and collision handled by the server/host, so the client just does local collision handling but only performs visualization of bullet impacts. This leads to the odd visually hitting or missing bullet that actually missed or hit - but this is normal behaviour in multiplayer games.
     
    Ohilo likes this.