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
  3. Dismiss Notice

Question NetworkObject with children (player list in a server lobby)

Discussion in 'Netcode for GameObjects' started by splundge, Mar 13, 2024.

  1. splundge

    splundge

    Joined:
    Dec 29, 2021
    Posts:
    24
    Hi
    I am trying to figure out how to keep the list of players with their teams visible to everyone in a lobby (before my game starts)



    - I have an image object (as a vertical group) with a list of players (you can see in the middle).
    - each time a player joins, i tried to spawn a new network object to insert into the list, but unity throws an error along the lines of 'NetworkObjects cannot be nested at runtime'.

    Right now, I am using an RPC to just 'instantiate' each list item when players join/leave.
    But I need to keep the player name a network variable, in case it changes.

    How would you typically keep a list of players visually available to every client? (like a score board in an FPS, a list of players in a lobby, etc)
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,896
    Parenting of NetworkObjects should be avoided as much as possible. I don't see an immediate reason here why the spawned net objects should be parented.

    If you do parent, you cannot simply assign transform.parent you have to use NetworkObject's TrySetParent() method and be aware of the parenting restrictions and behaviour.

    I work on that right now, at least the collection of players and their stats in a central place.
    This is what I do or plan on doing:

    • each Network Player instance have a NetworkPlayerState component
    • Players spawning and despawning register and unregister themselves from a DontDestroyOnLoad object ParticipatingPlayers - this could be a singleton. This gives access to players in a list for enumeration, and a way to access specific player instances via ClientId+PlayerIndex (in my project up to four local splitscreen players can join an online session).
      • Every client and the server naturally manage and have the same players list independently from each other. ParticipatingPlayers is a MonoBehaviour.
      • I may later extend this to have an ObservingPlayers component to differentiate between observers and participants, if needed. Or it might be a simple flag.
    • to display the players list, I would enumerate ParticipatingPlayers.Singleton.All to get each player instance
    • "player instance" likely does not refer to a GameObject but rather a central, commonly used component. Most likely it will be NetworkPlayerState because this contains all the data GUI and other systems need access to, like health, armor, powerups, keys, items.
    Note: my Player prefab is not the actual controlling player but a "managing" instance that takes care of adding/removing local splitscreen players and pairing them with their respective input devices. So for this kind of GUI I may not spawn the actual player prefab instance but rather manage this solely within the NetworkSplitscreenPlayers component. This would have 1-4 registered players and the GUI could use that list to see who will be participating. Only upon entering a map I would spawn the actual player instances and pair them with their input devices. Meaning: the list of players here wouldn't need to have actual network players spawned for each entry.

    Nice GUI btw. Though it looks like you made this with GameObject UGUI. Maybe it's not too late ... ;)
    You can get the same GUI done with UI Builder (w/o code) in maybe 30 minutes, it'll look nicer and will scale everything proportionally. Highly recommended!!

    What kind of game are you making?
    Looks a lot like I imagine the lobby setup for my first/third person Boomer Shooter Game Kit. :)
     
  3. splundge

    splundge

    Joined:
    Dec 29, 2021
    Posts:
    24
    Thanks @CodeSmile !

    Unfortunately I've just spent the last 4 weeks painstakingly crafting my UI using the out-of-the-box components like textmesh pro. I just needed to get some basic functionality together so you could launch the game, create/join a server. it took sooo long. I didn't realise there was a better toolkit to build ui. I'll have to take a look when i polish the ui.

    Also, thanks for the insight into how you manage the players across the network. I think i'll do something very similar


    It's funny you mention boomer shooters. My most favourite games were quake/unreal tournament/enemy territory. So the things I make are heavily inspired by the older style of fast-paced run and gun shooters. Anyway, this is my first unity game, so I'm trying to keep it simple while I get the hang of things. So I'm making a vehicle-based shooter
     
    CodeSmile likes this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,896
    Nice! :)

    Hmmmm ... why do I feel reminded of Desert Strike? Probably coincidence. :D
     
  5. splundge

    splundge

    Joined:
    Dec 29, 2021
    Posts:
    24
    Nah you nailed it. Inspired by the original return fire



    Had so much fun playing these as a kid. There's not many contemporary games in this genre. So i thought it'd be fun to try and make a top-down, multiplayer vehicle shooter (with ctf, deathmatch, objectives).
     
    CodeSmile likes this.
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,896
    That would be even cooler with local multiplayer support. Imagine 2 or even 4 helicopters roaming about. ;)
    I guess they'd have to be confined to within the camera viewport though. Splitscreen would likely make each player's viewspace unsatisfyingly small.