How To Make A Unity Multiplayer Lobby System In 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hello and welcome back to another Unity multiplayer tutorial today we're going to be looking at how to integrate unity's Lobby service within your multiplayer game so you can host and then see a list of all the currently hosted games then you can click on the one you'd like to join hit join and you're in if you end up enjoying the tutorial or find it useful then it would help a lot if you were to like And subscribe but without further Ado let's get started so as always there's a link down below to the GitHub repository for this project in case you just want to go download it to follow along or get the final project that's up to you it's down there in case you want it so inside of unity to start working with lobbies you're going to need to go to window package manager and download the lobby package if you haven't already so just go to Unity registry search Lobby and here it is install that and then you'll be ready and then once you've got the lobby package we can head over to ugs that's what we've been using for the previous tutorials we've set up relay using ugs and we're also going to go to lobby and then we can hit setup Lobby and we don't need to go through this whole process if you're just joining in this tutorial then you might need to go through each of these steps here but because we've already linked it like it says here you can skip this so next we've already got the lobby package next turn Lobby on next and then we're done finish so once you've enabled Lobby we can then head back to Unity again and what else have I set up well if we go into the main menu I've added a bit of UI as I usually do for the lobbies so I can enable this and we can see if I was to turn on the menu panel we have this Lobby window and it has a scroll it has a refresh button and then if we were to go to prefabs UI main menu I've got this Lobby item prefab which we can then spawn in so if I go to the lobby container scroll view viewport I can then drag in this prefab to the content and this is a Lobby item and I can spam duplicate it and once we get too many they don't actually render on the screen we won't see this below here we then get the scroll wheel that we can use to go through the list and yeah that's actually it for the setup I've just created this UI all we're going to be doing is hooking up this join button and we'll be changing the text here to say how many people are in our lobby and what the Lobby's name is so I'll get rid of those items and on the Lobby's background I've got a Lobby list script it's currently empty but I've just made the script here and same with the prefab that we just looked at so if I go back to the May menu Lobby item we have a script on there but that script is also empty so that's all you need to get going we can then go back disable the Lobby's background object so that it's off and same with the menu panel and to be actually able to turn that on you could write some code for it but what I've done is on the main menu we've got a lobbies button here now and that lobbies button all it does when you click it is it just sets the game object active for the background the Lobby's background so just have that button enable the game object that's all it is to turn on the lobby screen okay that should be it so we can close the menu panel so the first thing to do is to actually create a Lobby when a host starts a game so to do that we'll head to scripts networking and we'll go to the host manager that we have created and we'll go down to the method where we create a game so it's here when we create the allocation get the join code around about here we want to then make a Lobby so over here on about line 72 once we've set the relay data we'll do a try catch in case there's an issue here so try catch this and what do we want to do we want to create some Lobby options this is where you set various things about the lobby whether it's a public or private lobby and in our case we're going to set some data on our lobby which will be our join code so that anyone who joins our lobby then gets the code to actually connect to our server so over here we'll say VAR create Lobby options is equal to a new create Lobby options object if that's not imported just hit control period and import the lobbies and lobbies and models using and then in my case I want it to be a public Lobby so create Lobby option dot is private equals false and you can of course control this by a button in the UI so someone can make a public or a private lobby but for now we'll just have them all public and then create Lobby option dot data is where you can then set some custom data in our case it's going to be the join code so we'll make a new it has to be a new dictionary of string data object but inside of that you can do what you want so we'll have one element in there we'll call it join code this will be the key which is the string then the actual value will be a new data object and inside of a data object you simply set the visibility so this is going to be basically who can see it whether it's publicly available or whether you have to be in the lobby to get this data in our case we want to make it so you have to be in the lobby so data object visibility options Dot and in our case member so it's available to anyone who is a member of our lobby and then the actual value for this is going to be the join code itself so anyone who's in our lobby can read The Joint code let's make sure to put a semicolon down here and now we've made the settings for the lobby we can actually go ping the API to create the lobby for us so we'll make a Lobby variable is equal to and then we're going to be awaiting this since it's an API call we have to wait for the network latency so await lobbies dot instance dot create Lobby async and you pass in the name for your lobby and I don't currently have a way to actually come up with Lobby names so I'm just going to call it my Lobby for now for this test then the max players we've actually already assigned at the top is Max connections and the options are the options we've just made so that's create Lobby options let's make that plural so rename that to be plural there we go and then once this has been made we get back a Lobby ID so lobby.id and it'd be nice to store this in the script so we can use it later so at the top I'm going to go make a private string Lobby ID I already made this apparently I forgot to take this out but yes just make a private string Lobby ID and over here we can just set that so Lobby ID is equal to the Lobby's ID and then once you've made a Lobby it starts existing on their service but you have to keep pinging it every so often otherwise it'll just get cleaned up otherwise you might create a Lobby and never shut it down and it would just exist forever so you need to send a heartbeat is what it's referred to as so to do that we can make a co-routine that just happens every let's say 15 seconds and every time it does that it just tells them hey please keep our lobby active so to do that just below this method will make a private protein so I enumerator and we'll call it heartbeat Lobby co-routine taking in the time to wait for the heartbeat uh wait time seconds and then in here we just need to basically say new wait for seconds which you can use in co-routines passing in the delay wait time seconds and then if we store this in a variable so VAR delay equals we can then do a while loop so we can say while true so we make an infinite Loop here and we say lobbies dot instance dot send heartbeat ping async so that's a method in the lobby system you just pass in the ID which is why we stored it as a variable so Lobby ID and then after it's done it we just need to say yield return delay so we only create this object once but we reuse it every let's say 15 seconds and every 15 seconds it'll just ping the lobby so we can then go back up and over here when we set the lobby ID we can then say start Co routine heartbeat passing in let's say 15 for now and then if there's an issue in the catch we can get a Lobby service exception given to us and then we can just log that out so debug.log e and then we can also throw the exception so that's actually the whole process for starting up a Lobby and we can't really test it until we have the UI to see them and join so the next thing we'll do is we'll go to the lobbies list script over here so in here let's get rid of the start and update method and we'll say on enable so every time we enable this bit of UI so on enable we want to try refreshing the list so we'll make a method refresh list I'll make over here public and it's going to have to be an async void refresh list because we're doing more API calls and in the refresh list we only want to do it if we're not already doing it if we spam refresh we don't want it to let us do that otherwise we'll get some rate limiting calls so let's just make a Boolean at the top private ball is refreshing and then over here we can say well f is refreshing just return so we don't do it too many times and then right after that we can set is refreshing to true then at the end of this method we'll set is refreshing back to false and now we just need to do our asynchronous code in the middle here so now we need to make our search query to tell the lobby system basically what we want we want this many lobbies we only want the ones that are available that have like spaces available for us so to do that we can do our try catcher for the API call and then we can say VAR options equals new query lobbies options and if you spell that correctly you should be able to just import unity.services.lobby and then we can set on here options dot count this is how many we want to get let's just pick a number for now like 25 it really doesn't matter eventually you might want to add like pagination so you can say get me the first 25 and then get me the next 25 or whatever but we'll be happy with this for now we only have one and then we can set filters options.filters equals a new list of these now let's shorten this a bit so if we just remove all of the bit before it we can then just import unt.services.lobbies.models and then inside of here we can make a new query filter so the field if we go query query filter field options you can see all the things here that you can query for so we're going to say available slots then op is the operator so we can set query filter dot um so we want to get these are things like equal to not equal less than less than or equal to greater than greater than or equal to so we say we want any lobbies with available slots greater than so GT and then the value so let's say greater than zero this has to be a string here just how it is and then the host has the ability to lock a Lobby so we don't want to bother seeing any locked lobbies so right after this we can then say New let's get rid of this bracket here it's out of place so new query filter honestly I'm just going to copy paste the one we already have we'll get rid of the comma at the end put a semicolon over here so it's happy then we just need to change it from available slots to is locked and then greater than to EQ for equal to and then zero so if is locked to zero that's the same as is locked is false that's what we want we're all the ones that are not locked and that have available slots greater than zero and now we want to take this query and actually get some lobbies back so we'll say here VAR lobbies equals await to lobbies dot instance dot query lobbies async passing in our options that we made and then that will get back to us with some lobbies so now we're at the point where we want to spawn in the lobby items but we haven't actually written any code in the lobby item script yet so I'm going to hop over there for now and we're going to continue here so what does the lobby item have well we don't need to start an update what we do need is reference so Shirley's field private TMP underscore text this will be the lobby name text we'll have to import TMP Pro TM Pro and then we also need text a second time for the lobby players text to say how many people are there and then we can make a method that gets called whenever it's spawned in that we initialize it with the actual Lobby we want this item to represent which means we'll have to store a private lobby in here call it Lobby and then make sure we add Unity Services lobbies models then we can make the method initialize so public void initialize taking in the lobby that we are meant to be and because we have a button on here and when we click it we want to join the lobby we're also going to take in the lobbies list itself um lobbies list so whenever we're pressed we'll call a method on the lobbies list saying hey this is the lobby someone wants to join so we're also going to need to store that one privately so private lobbies list lobbies list and then we can just say this dot Lobby's list is equal to Lobby's list and same with the lobby let's start Lobby is equal to the lobby coming in and then for the UI we can update it to say Lobby name text is equal to lobby.name since uh lobbies have a name and then we can also set the player's text so Lobby play as text is equal to um whatsoever here okay so we need to set Lobby nametext.txt same with the players make sure it's dot text and then we can make a string here putting the dollar sign before it so I can use braces like this we're going to say inside of here lobby.players dot count slash lobbies oh sorry Lobby dot players uh here we go Max players and then we just need a method here a public void and this is going to be called when someone clicks on this button so public void join and what's going to happen when they hit join is we're going to say lobbies list Dot and then we'll need some method in there so join async um Lobby's list here should be lowercase I don't know why I made it uh uppercase there we go passing in the lobby this method we haven't made yet the join async method but we'll go make it right now so we're done with the lobby item we can go back to the list and at the bottom we'll have to make a public async void join async this is the method we just called in the item that takes in the lobby that we want to join and just like we have the is refreshing we should also have an is joining ball so we don't accidentally try joining two at once so let's make a second private ball is joining and then down in here we can do the usual if is joining then return and then we set is joining to true and at the end we set is joining back to false so to join a Lobby it's pretty simple we just do a try catch and then in the try what do we do well we say await lobbies dot instance dot join Lobby by ID async we have the lobby ID we want to join so it's Lobby dot ID and then I'm not actually sure if we can reuse this Lobby variable here so I'm going to make a new one via joining Lobby and once this is done once we've awaited it we can then go grab the join code that we set so string join code is equal to the joining Lobby dot data and we can read this data because we're now a member of the lobby and the key we set was join code and if you read the value of this you then get the join code that we set and now that we have a join code we can join like we would on the main menu if we just typed in the join code we can say await uh client manager dot instance dot start client and you just pass in the join code and then we can catch what's the issue here cannot wait voids so we'll have to go and make start client an async task and then make sure you've added fretting.tasks if you haven't already so now that is able to be awaited in the catch we can catch a Lobby service exception like we did in the other place debug.log Pro if there's an issue okay and technically if there is an issue and it fails to connect we should also set is joining back to filter otherwise we would then not be able to connect to another server because we would never set this back to false so now let's head back to Unity and let everything compile and hopefully that should be everything we'll have to go hook up our objects so we have this one warning let's see what it is so when we start client here we're not actually awaiting it we've now made it an async task so we should await it it won't break if we don't but we should make it not give us this issue and then this method itself will need to be asynchronous and now it's all happy cool so there's actually one last thing I need to do that I forgot so if we go back to the lobbies list we don't actually uh spawn in the lobby UI because we stopped at that point to go make the lobby item but now we need to of course spawn it in so let's make a reference to it at the top so serialize field uh private game object actually no because it's a Lobby item we want to spawn in a Lobby item call it Lobby item prefab and we need the transform parent to spawn it as a child of so private transform Lobby item parent and then once we've got all the lobbies we want to go through any that already exist because if we load some lobbies in and then go back and then come back again we want to completely clear it so for each uh transform child um in lobbies sorry in Lobby item parent then we can say destroy child.game object and then we can spawn in all the new ones so for each Lobby in lobbies dot results we can then instantiate a Lobby item prefab as the parent we can set it to be Lobby item parent and then we'll need to store this in a variable so we'll say VAR Lobby instance equals and then on Lobby instance we need to call our own initialize method that we made passing in the lobbies list which is this and the lobby which is Lobby and then we can just finish off the catch so it's going to be a Lobby service exception if we get an issue here we can then debug .log we cannot throw and just like below if it fails we want to set is refreshing to false otherwise we'll be permanently locked out of doing it so we'll do that there and now we're good we can go back there shouldn't be any warnings or errors and everything should be done so now we just need to hook it up so main menu Lobby's background we want to set the lobby item parent so that's content that's where we spawn in the lobby items and then the prefab itself let's lock my inspector go to prefabs UI main menu drag in the lobby item and then if we go to the lobby item we want to drag in the name text and the player's text and that should be good make sure the join button when you click it we then call Lobby item dot join and that should be our thing the only other thing I might not have mentioned is on this Lobby background we have a close button so when that close button is clipped I just do the opposite of what I do to open it I just call Lobby's background gameobject.setactive and then leave that false and then we also do have a manual refresh button so we could click that drag in the Lobby's background call lobbieslist Dot refresh list and hopefully that should be everything I know this was quite a long one quite an advanced tutorial with a lot going on but the repo is down below in case you need to actually go through it line by line yourself so let's do a file build and run and see if it works okay the build is done if we go into lobbies there shouldn't be any lobbies we'll close it we'll host and then once the hosting is done we should hopefully be able to go back to the client go to lobbies and we see in my lobbies there with one out of four players I'll hit join on the lobby give it a second it has joined us we can then pick our character and play like usual so yeah that's it for this tutorial we now have the lobbies working it was a very long and difficult tutorial with a lot of things to remember to do but it all worked in the end so I hope I managed to help some of you who are trying to implement lobbies into your own game to show you how to do it and of course there's more than what I showed you for example when we do the query for the lobbies I just hard-coded 25 and things like that you might want to actually make that a bit more robust and also when people close the game and leave the lobby you'll need to maybe add those cases in as well because right now if I just closed this client and then tried to look at lobbies again it would say you're already a member of that Lobby so there is a little bit of cleanup to do there but this video could go on for hours if I was to do every little thing so I hope this is fine for now if there are more features of lobbies you want me to cover then do let me know down below but yeah that's it for now so if you did enjoy the tutorial or found it useful then please leave a like And subscribe it would help a lot that's it for now so I'll see you in the next one goodbye but of course before I go I've got to thank my patrons a special thanks to Francisco Lira Ryan Shu David McDermott Gregory Pierce Kyle k Mark McCorkle and sahila if anyone else would like to help support the channel monetarily the link to patreon is down below if not there are also links down below to other social media such as twitch Twitter and Discord if you could help us out by following on any of those or checking any of those out that would be greatly appreciated that's it for now so I'll see you in the next one goodbye
Info
Channel: Dapper Dino
Views: 13,110
Rating: undefined out of 5
Keywords: unity multiplayer, unity multiplayer tutorial, unity multiplayer 2023, unity multiplayer 2022, how to make a multiplayer game, unity multiplayer game tutorial, unity networking, multiplayer networking, unity multiplayer game development, how to make a multiplayer game in unity, new unity multiplayer, new unity multiplayer system, lobby, multiplayer lobby, unity netcode, netcode for gameobjects, lobby system, create a lobby, join a lobby, create lobby, join lobby
Id: bNCzpKX4frg
Channel Id: undefined
Length: 23min 50sec (1430 seconds)
Published: Fri Mar 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.