Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Handle local UI in multiplayer

Discussion in 'Netcode for GameObjects' started by casteillet, Nov 7, 2023.

  1. casteillet

    casteillet

    Joined:
    Jun 29, 2019
    Posts:
    11
    Hi,

    I was searching online opinions for managing local UI in multiplayer and I just found multiple answer like :

    - UI should update itself by getting the local client at OnNetworkSpawn() and then I think it could subscribe to events maybe ?
    - Player if it is local player can FindObject the UI and set variables from the player

    What do you think is the best option ?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,010
    The first thing you ought to do is to decouple your UI from all other code. Put all UI code in its own assembly. UI consumes (depends on) other assemblies, but nothing else should depend on the UI except for, if needed, even higher level UI code.

    NOTHING in the UI should reference ANYTHING from the Netcode!

    The UI should not know whether a value it displays has been synchronized over the network, or was changed in singleplayer mode, or was modified through some other means (eg playback of an input recording aka 'replay'). The UI is a dumb state machine that receives value-changed events, and some state-change events (eg show the inventory). The UI should never synchronize any state over the network itself, or directly register with network synchronization events, because that tremendously increases the UI code's complexity and side-effects.

    Then the UI registers for events in the respective subsystems (eg Player, World, GameState) that it is interest in, eg. "OnPlayerHealthChanged" or "OnBeforeSceneChange" or "OnEnterCombatMode" etc etc

    Player-specific data the UI needs to display is registered through the "Player" class that handles the local player stuff. Everything else, like other player's data, names, hitpoints, and such would be handled by a NetworkGameState class the UI can register for event updates.
     
    Last edited: Nov 7, 2023
    RikuTheFuffs-U and casteillet like this.
  3. casteillet

    casteillet

    Joined:
    Jun 29, 2019
    Posts:
    11
    Thank you for this clear answer
     
    CodeSmile likes this.