How To Make A Multiplayer Game In Unity 2021.1 - Lobby

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you so much for the video! :)

👍︎︎ 1 👤︎︎ u/dragonfortgame 📅︎︎ Jul 22 2021 🗫︎ replies
Captions
okay so hello welcome back to another unity multiplayer tutorial today we'll be creating a lobby that players can join they can then ready up or leave and then once everyone is readied up the owner can start i hope you're looking forward to it so let's get started so that we can focus on the lobby today i've already set quite a few things up and i'll quickly show it now but if you want access to it it's all free available on the github link down below in the description so i'll quickly show you around we have free scenes now we have a menu scene a lobby scene and then the main scene which is where the gameplay takes place and if i go to my scenes we've got here's the main menu you're looking at now we've got the lobby here and then the actual gameplay scene is here and there's currently nothing in it but we'll be moving on to that next time so all we need really right now is a main menu and a lobby if you're not comfortable creating your own user interface then like i said you can go down to the github and just get what i've got here it's simply just a rectangle with these four cards for each player and if i open this up we've got the four player cards and each player card is a prefab so if we go into the prefabs let's have a look here um we've got the background the waiting text and then we've also got this panel which is disabled and when we turn it on it has the name of the player and then it has also a place for you to put maybe an image of the selected character and then a little checkbox at the bottom we can toggle over here if i go down and this can say whether the player is ready or not and then if i go back we have these buttons at the bottom everyone will have a leave and a ready up button and then the host will also have a start game button and that will be enabled once everyone is ready up if we go back to the menu now you'll see i have the network manager and this is just like normal it has the registered scene names but i've turned off create player prefab so that when players connect to the lobby we don't spawn in any player object for them we'll be doing that manually when they actually join the game and then we also have the net portals and you'll see i have a game net portal then a client and a server one and this is just to split up the logic for connection disconnection storing client data all that kind of stuff in the previous tutorials i've done it in a single script so for example in the player names tutorial i've got this password network manager and this handles the events for people connecting disconnecting but it can get pretty cluttered because you've got client specific server specific shared it's all in one script so i've done it the way unity does it which is having a client game net portal for client specific logic and then the server and the shared game one there's quite a lot of code here that isn't specific for this lobby tutorial so i recommend if you want to follow along with me or just understand what this all does go and look on the github you'll find it in this project and we'll be focusing on the coding for the lobby today the only important bit for today is in the server net portal we have some dictionaries here storing the client's data and if we look at what that actually stores it's their name and their client id and then back in here we have a method called getplayerdata which will try and basically find for a client id it will find their player data so we can say get us the data for player 2 and it will go find it and it will return their name basically and if we wanted more data with this we could just add it in here too i think that's it for the setup so let's head over to the lobby scene and we can focus on this today so on the lobby ui game object we have the lobby ui script and i've already set these references up so the script right now is just an array of player cards and a button and all a player card is is just a script with all the different bits on the ui so we can toggle on and off the panels we can update the player's name text and their toggle and all these kind of things so so far i just have all the data in here for both of these scripts so for our lobby to work we're going to need to sync data to all the different clients so it can display who's in which slot and lobby whether they're ready or not and what their name is so we're going to need to have a networked variable and usually you would use a networked int or a network string but we need this to be dynamic because it could be anywhere between two and four players in my case or any technically one and four players as they're joining the lobby so we need to use a list and ml api has a class called network list over here of type and you need to pass in a type but it can't be any old type it needs to know how to serialize it so what we can do is we can make our own type called lobby player state like this and then to explain how to serialize it what we can do over here is we can make our actual data so we want to store a client id so client id we want to store a name this is what we're actually syncing to everyone so their id their name and whether they're ready or not so that'll be a bool is ready and let's make sure the play name is a string not a euler and now to actually explain how this works is we need to create a constructor so public lobby player state and this will take in a yulong client id a string player name and a bool is ready and we just need to set all these so client id equals client id make sure you get the casing right here uppercase player name is equal to lowercase player name and same with is ready for ml api to let us create our own data type we need to make sure to implement the interface i network serializable like so import mlapi.serialization and it's going to ask us to implement this method called network serialize and this is how we actually serialize our data so we take this serializer that it passes us and we call the serialize method and we pass in ref and then the first variable so client id and then you just do this for all of the other variables so player name and is ready and there's one last thing which is we need to make this a struct since this is going to be a value type not a reference type and i can zoom out again and here we are so we've now created our own data type that we can sync across the network and the way we do this now is we pass it in here so this is the lobby player state and we'll just call this the lobby players equals a new network list of lobby player state and we have to make sure we've got the correct using sir there we go so now whenever this list is updated on the server it will be synced to the other clients which is really useful so let's start off by now writing some methods so we're going to override the network start method and the first thing we want to do is say if we are a client then we want to subscribe so lobbyplayers. on a list changed and we can give it a method handle lobby players state changed and we'll implement that method so what we're doing now is saying if we're a client when the list updates do something and in our case we want to update the ui let's finish off the start method so we're then going to check if we are a server then let's enable the start game button because they're the host so start game button door game object got set active true so they can now see that button and we also want to start subscribing to some events because we need to know in the lobby when players connect and disconnect to then update the lobby so network manager dot singleton the on client connect callback and we'll make this method handle client connected and we'll do the same for disconnecting on client disconnect callback handle client disconnect and then we'll take both these methods and we'll create them so generate this method and generate this method the final thing to do in start is to go over all the players who are already connected and add them because these events will only trigger when new players connect or disconnect but what about the players who are already there so let's loop over them for each network client and we'll call it client in network manager dot singleton and we have this connected clients list so we can loop over all of the connected clients let's make sure we have the name space and then we just need to call handle client connected manually so client dot client id so this will basically do what the event is doing when someone connects but it just does it manually for all the people who are already connected and then we want to make sure we clean up correctly so let's say private void on destroy so unity will call this when this object gets destroyed and we just want to unsubscribe from the list so unsubscribe make sure we change it to a minus and then we can say if network manager dot singleton so if the singleton still exists then we want to unsubscribe from these two events and this mainly happens when you stop running the unity editor and it will stop you getting errors about memory leaks or you know things not being unsubscribed correctly so just make sure we unsubscribe from all our stuff we then want to make a method to check if everybody is ready so let's make a private bull is everyone ready and what it's going to do is it's going to say well let's check how many players are in the lobby so lobbyplayers.count if that is less than in our case two we'll just hard code it to b2 for now return false because there aren't enough players and if there are enough players we then need to make sure they're ready so for each player in lobby players let's make sure we put var for each player in lobby players if that player is ready so if the player is not ready then return false but if we get to the end of this method it means we have enough players and they're all ready so return true and now we can deal with people connecting and disconnecting so let's take the connect method move it above the disconnect method and rename the parameters to client id and we want to go find the name of this player that is connected so we can put it in the list so we'll say here server game net portal import the namespace dot instance dot get player data and if you pass in their client id you get back their player data so var player data equals that and then we need to make sure it actually exists if we succeeded in getting it so if not player data dot has value then return and this is identical to doing is equal to null but it's using a c c-sharp nullables and this isn't the time to get into that so go look up c-sharp nullables if you don't know about this but this is basically the same as saying if it's equal to null return if it's not equal to null then let's add to the list of lobby players a new lobby player state and we'll say here the first parameter is the client id the second is their name so playerdata.value.playername and whether they're ready or not we'll default that to false and then put the semicolon and we're done with this method then for disconnecting we just want to loop over so make a for loop loop over all of the lobby players count and we're going to say here if lobby players i client id matches the client id for the person who disconnected then that remove at i so just remove that player and break because we can stop there it's pretty simple for disconnecting and the next thing we need to do is give the players a way to actually ready up so we'll make a method here private void and we can call this toggle ready and it's a server rpc it's an rpc we're going to send to the server so we need to on the line before put the attribute server rpc and in our case we're going to say require ownership equals false so anyone can call this method they don't need to own it but anyone calling it we still need to know who it is that called it so what you can do is you can write server rpc params and we'll just call it server rpc params equals default this is the syntax and what we can do now is anyone can call this method and we can read server rbcprems dot receive dot sender client id and this is the id of the person who sent the request so this lobby script it's not like each person has their own instance of this it's like a shared one so anyone can call this method but we still can find out who called it which is useful because with this what we can do let's just get rid of it for now we can say when someone reads up let's go over all of the players in lobbyplayers.com go over all the players and if lobby players i if their id is equal to the person who sent the request then we need to update their data because their name stays the same their id stays the same but whether they're ready or not gets toggled from true to false so lobby players i equals a new lobby player state where the client id stays the same so i'll be players i dot client id the name stays the same so a lot of players i dot player name and then the israeli changes so we'll just set it equal to not lobby players i dot is ready which will just toggle it to be the opposite of what it already is the next method we'll make is a way for the host to start the game so we'll make a server rpc here that doesn't require ownership again and we'll call it private void start game server rpc and we'll take in the params again equals default and while this isn't necessary when the host is the only person who can start the game this is a good way to get ready for when the uh anyone can start the game so for example if you have dedicated servers and you might want it to be the first person who joins a lobby can start the game you would change this logic accordingly but for now we're going to say if the person calling this method does not equal our id so networkmanager.singleton.localclientid then return so this is saying if any of the other clients try to start the game then return but if not then it means the host is trying to start so then we'll say well if not everyone er is everyone ready here we go then return so we need to make sure everyone's ready and if they are then we can call servergamenetportal.instance.startgame which is a method i've already created like i said you can go find it on github it's literally just set a bull to true so people can't keep connecting and then load the main gameplay scene only a few more methods now this is quite a long tutorial we've got to make methods for all the buttons so public void on leave clicked public void on ready clicked and a public void on start game clicks so for leaving we say game net portal dot instance dot request disconnect and that will handle the whole disconnection flow then for ready clicked toggle ready server rpc and first start game clicked start game server rpc and then finally the handle lobby player state changed we'll say here lobby state we'll just change the name of that and we want to now go through all of the player cards on the ui so for every lobby player cards.length we want to say if there are more players so if lobbyplayers.count is greater than i that means that we should populate this card and if it's not greater than then it means we shouldn't because it means we don't have enough players so what we can do here is we can say lobby player card or cards lobby player cards i dot and we don't have the method yet but we'll make a method called update display and we'll pass in the data for lobby players i and then we'll say else here so if there aren't enough players we then want to turn off that card so lobby player cards i dot disable display and then one extra thing needs to happen over here if you are the host so if is host we also want to update not just the cards but also the start game button so i'll say start game button dot interactable equals is everyone ready that method earlier that returns a ball so if everyone's ready the button will turn on and if they're not then it'll turn off and now we just need to make these two methods the update display method and the disable display method let's go over here now and we'll just make these public voids and all we need to do in here is say well to update the display playerdisplaynametext.txt equals lobbyplayer state dot player name and then is ready toggle dot is on equals lobby playstate dot is ready so just update all the ui elements to match the state uh waiting for player panel that's set active false and player data panel that's active true and then we just take these last two lines and when we want to disable the display we just do the opposite let's turn on the waiting for player and turn off the player data so yeah that should be it for the coding we still need to hook some stuff up inside of unity and give it a test and i hope i've explained the lobby code well enough today of course there's quite a bit of code that i didn't go over which is the net portal stuff which like i said is in the boss room sample and it's also in my github so you can go and find out and read into how it all works and what it does but what we need to do now is go into the ui over here go to the buttons so the leave button and when we call leave we just want to say love ui on leave clipped and then we've got the ready button to do the same thing on ready clicked and finally the start game button hook that up on start game clicked make sure this is yep it's all synced let's go to our scenes go back to the menu scene and we can do a little quick test so we can hit play and we can see if it works for ourselves so enter name dapper post and you see we get dapper here and if i hit ready it reads up and i hit unready and it doesn't and because the min players are two it doesn't matter if i'm read up because we've not got two players if i leave i go back to the main menu so let's stop running it and let's give it a build all right the build is done let's run it in the editor and we'll host over here so we'll say enter name when it loads we'll say dapper host i'll go over here and we'll say dino host oh sorry dino client and when i joined you saw it joy on both it's synced and if i ready up you see the ready up is syncing back and forth and if i ready up over here both players are ready so we can start and if i join as a client over here and call myself dino2 join as a client now i'm joined and the host can't start because they're free players but only two already of course if i ready up that does what you'd expect and all the free clients see everything synced in real time so yeah that's it for this tutorial i hope you enjoyed and found it useful if you did then please leave a like and subscribe if you want to see how all the net portal stuff works it's on the github and it's also in the boss room sample too though mine is a little bit different to that and like i said that's just all the boilerplate stuff it's nothing lobby specific we've just built on top of it let me know down below what you want to see next thanks as always for watching and i'll see you in the next one goodbye but of course before i go i've got to thank my patrons and special thanks to francisco lira sahila david mcdermott evan maxie gabe torres gregory pierce yuri sletter katinka mom lauren simpson malvin mark mike miller wreck andrew williams chris diplock theory and dario if anyone else is able to help support the channel monetarily 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 flying on any of those or checking any of this out that'd be greatly appreciated thanks again for watching and i'll see you in the next one goodbye
Info
Channel: Dapper Dino
Views: 16,884
Rating: undefined out of 5
Keywords: unity multiplayer, unity multiplayer tutorial, unity multiplayer 2020, unity multiplayer 2021, 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
Id: sBR0oJJjx6Q
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Thu Jul 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.