Search Unity

Question Logic on instantiating player's characters inside a lobby with the positioning

Discussion in 'Multiplayer' started by IncrediblyWalrus, Oct 10, 2022.

  1. IncrediblyWalrus

    IncrediblyWalrus

    Joined:
    Jul 22, 2019
    Posts:
    46
    Hi there,

    I'm trying to figure out in my head how I'd go about the logic on:

    - Player joins a lobby
    - On the entering of the player, their character will show up within the lobby

    -- Players stay inside this lobby until they ready up.
    Basically, for reference, like how Fortnite does it with their lobbies.

    I know the logic on Instantiating the actual players characters, however, what I'm trying to wrap my head around is positioning those instantiated objects onto set positions within the lobby, and having them not overlap. So the instantiating objects I know of, it's the positioning them onto different transforms PER character/player joining.

    What I'm probably wanting to do is have an array of transformations, and for every character instantiated (on player join), have the new player spawn on the next transformation within the array, and not the previous one that was used. Does that make sense? I'm unsure how to really go about doing this in code logic.

    If anybody could give me some insight that would be helpful, or if my terrible explanation is still terrible and you need further information please let me know.

    Thanks.
     
    Last edited: Oct 10, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    This should help you get started:


    I believe the author uses fixed slots for each connection so if 3 clients are connected and the second disconnects and reconnects he will appear in slot 4. But it shouldn't be too hard to fix this.

    You basically only need two arrays, one with the positions and one that indicates which slot is taken by a player. When a client joins, you go through the second array to find the first slot that isn't taken, then you use that same index to get the position from the position array.
     
  3. IncrediblyWalrus

    IncrediblyWalrus

    Joined:
    Jul 22, 2019
    Posts:
    46
    I saw that video, but I didn't click on it due to me assuming they just used the horizontal layout component as it's UI based, not 3D (at least in the thumbnail). I'm going to check it out later on tonight!
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Cool!

    I'm also just getting around to moving on to Lobby and Player spawn and handling all the Lobby logic. I will post that online for free in ... a couple days probably.
     
  5. IncrediblyWalrus

    IncrediblyWalrus

    Joined:
    Jul 22, 2019
    Posts:
    46
    I haven't had the chance to look into the video due to being busy travelling around countries. Please do post on this thread if you do, I don't mind if it's just the logic behind it with no code snippets. Reading other people's logic on doing things just helps me understand a little bit more.
    I'm using Photon in this situation, unsure if that's the library you're using too.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    I use NGO. But maybe it's helpful anyway.

    You can download, fork or clone the project here:
    https://github.com/CodeSmile-0000011110110111/UnityNetcodeBiteSizeExamples

    I'm working on Lobby right now, might need another day or two until I'm happy with how that works.

    I will promote this project soon with documentation walkthroughs. There is a lot to be learned from implementing things in a general way and trying out all sorts of things for common uses, not just specific to a single game. That way, I get a better understanding of what would be common issues and resulting best practices from that.

    For a similar reason I'm also not looking into projects like Boss Room: it's too complex to find an answer to a specific issue, at the same time core features are too game-specific - they specifically say so about their Action system.
     
  7. IncrediblyWalrus

    IncrediblyWalrus

    Joined:
    Jul 22, 2019
    Posts:
    46
    I see, just checking it out now. Your scene management is actually how I've gone about doing this myself, interesting about the scenes loading too quickly. Were you checking to see if everything was done loading such as scripts, assets, etc?

    I might try and look into only loading the scenes once everyone has loading everything in, that way, the players aren't walking around waiting for one or more people with poorer writings to load into the game. This would work in my case due to it just being a party game, 4 players in a room.

    Wonderful idea though on studying things, writing it all up to understand more, I look forward to reading more into your post.