[2] Multiplayer FPS in Unity: Room & Player List

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're gonna be working on listing rooms in the menu because right now we can create rooms and we can name rooms and we can view the room but we can't actually join any rooms and that's gonna be important for when we play the game so I'm gonna start here by making a new menu I'm gonna call it find room and yeah and add a menu component to that call it find room and then save lower loading money so you can edit this I'm probably gonna actually take the air text at the top or name it title text and we're gonna call this find room and then here I'm going to create an image which we can use as our room list background be 600 and then I'm gonna make it stretch to fill vertically bit of spacing on the top nice spacing on the bottom and then all make it just a little darker than the background like that okay I'm gonna rename this list content and then at the blood at the bottom the bottom here hanging at a button back button all right I'm gonna set up this button to title and I just notice in here actually it was too close many and there should be open all right so now we've got our menu set up let's add the actual code I'm gonna open up my launcher script here and in my launcher script I'm going to add a new override method called on room list update and here the bottom public override floyd on list update and this is gonna give us a list of room info which is photons room information class this is gonna give us all the info about our room like the name the max number players any properties I wish we might add in the future alright so what I want this to do is when we get an updated version of the room list I want to destroy all the current buttons that we have on our list and then add the new buttons for the rooms so I'm gonna make a new serialize field transform room list content and then a new game object room list prefab list hey don't be found all right so I'm going to go back into our menu here and actually add a vertical layout group opponent to this get stretch on the width and add some spacing and padding to a 10 on each side I'm gonna make a new button here so at all I'm gonna set this font size to 45 and I know there's a bird name and this button is are gonna be our room item prefab so I'm gonna create a new folder prefabs and drag our button in there rename it room list item and then on our prefab here I want to stretch it out just so we can see it in the prefab editor I'm gonna add a new script called room list item all right and I'm going to open that up in Visual Studio and really all this needs to do is set the text the name of the room and then when we click it we want it to join the room that's all our room but it needs to do so I'm gonna make a new public void setup and then room info press alt enter to form a clip or hooked on dot real time we're gonna just type this in visual studio just nicely Auto automatically does that for me Benefield info and then I'm gonna sterilize field for our text and then a public void on click and this is where we'll join the room and buttons click so for setup I'm just gonna say next our tax equals info dot name and then also include a reference room info info we can make this an underscore so it's separate from our global variable and equals and so so we're assigning the room info we got in the setup method to our variable in our class so that we could reference it later in the on click so that we can join it so now in our launcher when we get a room list let's loop through the list and instantiate a room list item prefab and the list content not get component their own list item that's setup room list I there we go so that will instantiate our room list item prefab in our room list item container and then we'll get the rimless item component from it and call the setup method with the room info that we want to assign to it all right and also gonna do for each transform friends and list content I'm just going to destroy that so that we clear the list every time we get an update now let's go back into our menu our title menu I actually want this find room button up at the top here to tell our menu manager to open find room then you find room I have to have this turret list of menus alright now let's assign those references in the inspector here this content and the realist item prefab and we can disable this and re-enable are loading menu as the start back in Visual Studio I want our on click to actually join the room so in our launcher we're gonna make a new public void join room room info info we're going to call photon network dot John room and we're gonna say info dot name and then we're gonna open the loading mini because it'll take us up at join the room and then in our on join room function which we already have here it's gonna open the room menu and update the room text the room name text that's great so that functionality is done now we just need a way to call that method so I'm going to make this launcher a singleton to look static launcher instance and are awake instance equals this and then in our room list side and we could say launcher instance dot join room info there we go so that functionality is done now find room there we go sent to the finder of menu if we click back it goes back to the main menu that's great now in order to test this I'm actually going to need to build the game so that we can run it separately from the unity editor so that we can create a room and then join it from the other application so in my project settings here I'm just gonna set the default full screen mode to windowed and then we're going to leave it at that so that I can move it on my screen alright so on our room --less item we now have to assign the actual text component here so I'll get an air-filled it and go into file build settings make sure my scene is at it and then build it run I made a new build folder in my my actual unity project and you can build it anywhere doesn't really matter but I'm gonna select that folder and build it there I just like to build it in my unity project folder because it keeps it more organized alright once it's built it'll automatically run because we clicked build and run and once we're in here I'm gonna try and create a room room test create and then back in unity I'll run the game and we can do find room and we can see the room here room test if we click that then nothing will happen because on the room list item we also have to in this on click event assign the room list item the list item on click alright great now back in our scene if we run this find room click it will join the room right now we're in room test both are built client and the unity editor are both in the room test and the next step will be listing the players in the current room alright so since our player list is going to be so similar to our find room list I'm just going to copy this room this content and paste it into the room menu have that in the middle great and then I'm gonna make another prefab for our player list item here and I think that I'm just gonna make it a text object because all we're displaying is our user name we don't need a button or anything so I'm gonna make that centered probably and then I'll say player name and we can rename this layer list item is that of to our prefabs all right now on our player list item here I'm going to add a new component call it the player list item and I want the player list item to be responsible for destroying itself when the player leaves the room so basically the architecture I'm thinking here is when a player joins the room the launcher will receive a call back and it will instantiate the player list item and then when a player leaves the room the player list item will register that and then destroy itself so we're also going to have to derive this from monobehaviour pond callbacks make sure you're using photon dot pond and then I can say public void set up flare flare and we'll have to use photon dot real time - all right I'm also going to include a reference to the player here player player we're gonna use later like this underscore so that we can tell the difference and will sign it here and then we'll need a reference to our text text is fine text text equals player dot nickname okay and we haven't actually assigned a nickname to our players yet so we'll do that after this now we also want to do public or I avoid on player left room and this is going to take the parameter player other player and that's going to be the player that left the room so we're going to compare if our player is the same as other player then we can destroy this game object because our player left the room so we want to destroy this and then we also want to do public override on left room we want to destroy ourselves too as if we leave the room then I can't see the players in anymore so we'll just destroy that now in our launcher I'm going to include a reference to our player list item prefab and also our player this content so now that we have these referenced when we see the call my public override will an aunt Claire entered room instantiate I'll just copy this Stan she ate layer list item fab player list content I get component player at least I know that's setup new player all right now in our launcher also just for now when we have joined the lobby we're just gonna set our photon network name to be player less random range 0 [Music] 1003 and this is gonna just generate a random name for our player which we'll use as a placeholder until we implement an actual user name system so on our player list item you can delete that from this and also name this player list content on our canvas we can assign this content player list item and then our player list item here scaled up further viewing and we can make sure we have the player list item a component on here and assign this is the text great now if we don't start it with that open that's wrong let's start with that loading when you open all right start the game to create a room test create room so actually I've just probably the only override we have here it's when a player enters the room and that's not going to be called when we entered the room that's only I me called on another player enters the room so we also need to make sure when we join the room that we just that we instantiate a new player lo sign for all the player so I'm gonna make a loop here float on network player list dot count let's actually store this so we don't have to retrieve it twice and the player is done account for each player in our player list we're going to instantiate a new player list item and set it up with players I so we're going to loop through the entire list and create a new player list item for every player in that list funny thing is I actually out of this or the wrong function it should not go in join room it should be in on joined room or override this is when we click on a room button this is called and click a room button not when we actually join the room so now the creator of test room there we go there is our player Nate in the room this is gonna be the list of players so if I build the game with control B and then I can run the game of the unity editor to create a round test great room and then I'll do find room just join that room and now we can see that on both screens we can see that the players have actually joined and if I run this again and go to find room then I can find the room and bam now all three of us are in and it syncs up our names alright that's great and that's going to be the end of this episode we covered joining rooms and also listing players in the rooms [Music] thirsty [Music] [Applause]
Info
Channel: Rugbug Redfern
Views: 48,380
Rating: undefined out of 5
Keywords: tutorial, multiplayer, fps, photon, pun, pun 2, unity, development, devlog, help, online, gaming, shooter, first person shooter, gun, shoot, connect, bullet, photon 2, how to, how, create, code, c#, unity3d, csharp, walkthrough, lesson, lecture, online multiplayer, multiplayer fps, unreal, godot, series, open source, source code, multiplayer fps tutorial, photon tutorial, photon fps tutorial, pun fps tutorial, pun 2 fps, 2020, 2021, new, updated, c sharp, unity multiplayer fps, multiplayer fps in unity
Id: KGzMxalSqQE
Channel Id: undefined
Length: 17min 2sec (1022 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.