Managing Connected Players | OnPostLogin, OnSwapPlayerControllers | UE5 Multiplayer Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome to this new tutorial so it's been a while since my last tutorial and in my last tutorial we talked about the multiplayer framework in that video i showed you all kinds of classes for unreal engine and i also showed you the relationship between these classes so basically we had this blueprint there and in this blueprint i explained what for instance a gamemode is and what it is useful for i also explained the location of the gamemode so that the game would only exist on the server for example then we took a look at the game state and i explained that that exists on the server and the clients we took a look at the player state we took a look at what a pawn is or a character class blueprint i explain what a player controller is and that it exists on the server and on the owning client and then i also explained the use case of for instance the player controller and these other classes and then finally i explained what widgets are and that they exist on the owning client and also what the relationship between widgets and the player controller are and in this video i also gave you these little examples so for instance i gave you an example of respawning a player so the example was if in the player controller you press p for example you call a function called respawnplayer and on the server that will then call a respawn function that exists inside of our gamemode and inside of the gamemode that little function called spawner player would then actually spawn our player and then i also explained why we do it like this so why do we call from the player controller something on the server and why does a function like respawning or why do i install it on the game mode well so that would just keep that those kinds of functions away from the the clients as much as possible basically and since the game mode is intended to code your rules something like respawning can be seen as a rule and that's also a good reason why we install something like that on the gamemode for example so okay now now that we looked at all of this basic framework i think it is important that we start to look at how we can actually apply this in our multiplayer game uh and that's the reason for this video in this video i want to basically explain how we can properly set up a basic setup for your game so once you download our engine and you want to get started with multiplayer what is the type of setup that we should do and what's the code that we need to write in the beginning to make our multiplayer game work and have a good functioning setup that we can actually build and expand upon so let's take a look at that and let's actually see how this is how this is all applied here so what i've done so far is simply inside of my content folder we got this little tutorial folder here here i got all these tutorials you can download this project on our patreon as you guys know so if you want to have access to this you can get it there so right now we have this tutorial based setup folder and we have a blueprints folder so the first thing we want to do as explained in last video is we want to assign a game mode to this level because the gamemode controls all of our other classes and in these classes we can code the logic for our game so the first thing that we're going to do here is right click go to blueprint class click on all classes here and we're going to type in game mode and then as explained we do not want the game mode base because that's better suited for single player games but we want the actual game mode class because that has some handy features for multiplayer so let's select game mode and click on select and then i'm gonna use my usual acronyms here so that's called bp underscore gm underscore and then you can give your game mode a specific name so you can have a gameplay game mode you can have a main menu game mode etc etc and of course your game can have multiple game modes if you have for instance a free-for-all game and a team deathmatch game for example so in this example i'm just gonna call it bpgm base setup since this is like a base setup tutorial now we can go ahead and assign this blueprint here to this level so if i select it i can click on this little dot here and now my bpgm base setup is assigned to this level you can also simply click on the drop down here and find your game mode over here now like i said this bp unscrew gem it's an acronym so you can call it whatever the actual name doesn't matter but this is just a way of structuring your projects and since we also make marketplace products this is the setup that they require us for the marketplace that's why i use this setup here okay the next thing that we're gonna make is let's make a game state so let's type in game state yet again we do not want the base game state but we just want gamestate class so let's click on that and call this bpgs for gamestate and then our name here base oh base setup all right then we want to assign the base setup to this gamemode base so let's click on gamestate here click on base setup then the next class that we're gonna make is a player state so let's type in player state here we go this one click bp underscore ps underscore base setup and then the next thing right click here is a player controller you can find that one on top here so let's type in bp pc base setup okay and then we still need to assign our player state and player controller so player controller click it here paste setup and here you see player state click it and click on base setup okay now that we have all of this defined we can also define a character here but what i want to do and what is a better setup for code is that you do not define your base character in here because right now i could just go for my third person character which is the enrange of mannequin but actually i want to set this to none and the reason for that is if you make a typical game it's not gonna suffice to just install your class here as a default pawn and the reason for that is that let's say that your pawn dies then what happens because this only works as soon as you click play one time then you typically spawn with your character so if i install my character there like this one and i click on spawn then i become this guy if this guy dies now then what so because of that simple reason we already just want to by default click on none and we want to spawn this character with code okay so how do we go from here um well obviously in a game you do need a character so my character's over here in the assets folder and then we go to templates then i use the third person character and this character has some simple code all it is is some simple basic code to make the character move basically so nothing special but it's good to know that we have a character here that we can use okay let's get back to it so what's the first thing that i typically do when i create a multiplayer game what is my basic setup well first thing i do is that i go to the game mode and the reason is because in a multiplayer game obviously your players are going to connect and they are going to disconnect and once a player connects and disconnects i want to store that player somewhere so that on the server if i am for instance the host of the match or if i'm a dedicated server it doesn't really matter i just want to have an overview of the players that are in my session at the moment so that i can control them have information of them and so that i can share that information with the connected clients a good example of this is for instance let's say you have a player list i want to have my character in the world i want to press a tab and i want to see a player list of the connected players so because of that i need to have that information on the server and a good place where you can get that type of information is for instance here in the game mode you could store the information here so that you can control these players so another place where you can store that information is here in the game state since it also has to do with the state of the game and here is actually a variable that you can call that contains all of these players so we'll get to this in a minute but first let's look at the game mode where we will start up this setup so if a player connects then what happens because we see here we have begin play and we have eventiq well in the game mode and only in the game mode we have an event called on post login so here we see under the game category we see event on pause login if we hover over it it says notification that the player has successfully logged in and has been given a player controller so the moment so let's click this so the moment that a player actually connects to the session via for instance just joining the session right so you host the session you find a session then you connect to a session then inside of the game mode which only exists on the server this event here gets called because it says here notification basically so this gets called this notifies the game mode that the player connected and that the guy has a player controller so out of new player here we also see that this is an object reference of a player controller so what does this mean well as soon as this guy connects to the session this this happens and the guy that connects is this player controller so what can we already do here well we can store a reference of this player controller so that we already know about one player who that player is but instead of storing a reference here let's store an array so that we know for each player that connects who it is so the first thing that we're going to do here if a player connects we're going to make a copy of that player so let's go ahead and right click and promote it to variable and if we click away we see here this is called new player and it's of the type player controller well this is just one player controller but we want to store every single player controller so let's go ahead disconnect this select this variable and what we're going to do is here on the top right if we click on it here in the top right instead of having it just be a single we want to be an array so let's change the variable type all right now let's hit compile and select variable again so what is an array well in an array we can store multiple players right now so all these players can go into this array and then we have an overview of everybody that connected basically and that overview will look like this if i click here on the plus here you will see all of these player controllers that are connected so how do we do this how do we store all of these player controllers here i just cleared it out by the way we grab this variable into the scene here and then we drag off of it and we type in add and then we can click on the add here and that's how we can add this player that's incoming to this array of player controllers so let's add it like this and then let's actually rename this variable because now it's called new player so let's call this something like all pcs which stands for all player controllers this can also be called all players okay so now every time a player connects i have a copy of that guy inside of this variable which means now on my server i have all my players that are connected in here and why do i want that well let's say i want to make for instance i want to send this information to the connected clients at some point or let's say that a timer runs of 30 seconds and after 30 seconds i want the server to spawn everybody at a certain location because the game is about to start then the server can now do this because the server has this information so how would the server do something like that well if we hold f and left click we get it for each loop and now the server could do a certain functionality for every player that is in here so here we get a player controller and we can have all these player controllers teleport them to a certain location for example so that's why it's a good reason to store a copy of all your connected players then let's look at another node which is called on swap player controllers and this function over here if we hover over it it says called when a player controller is swapped to a new one during seamless travel so what's the difference between this one and this one well we see here that we have a reference of the old player controller and we have a reference of a new player controller and like i said during seamless travel this event here gets called so what is seamless travel well let's say that you are in your game at your session which is a lobby players then connect they then connect on post login gets called because this as it says here when you log in and this login only happens once when the player connects now let's say that you as the whole server you guys travel from a lobby level to a gameplay level at that point you are already logged into this session so this one will not get called again but i do leave from one level to another level which means i go from one actual physical level like this to another physical level which means my game mode over here also gets reloaded and is completely new and you know what that means once i go to the second level this variable here where we just added everybody is now completely empty so how do we then basically get that variable again well you guessed it if we swap the level we want to just do the same thing with all the new player controllers we want to add them here as well so when we travel level i still have a copy of all of my connected players alright and now let's go for the last situation which is if we are inside of that lobby let's say that players disconnect because here we just add somebody every time they connect what if they disconnect then my array here which contains the information of the connected players is no longer up to date ok so to handle that we have an event which only exists inside of the game mode just like these two other events which is called on log out and it's also of the game category and if you hover over it it says implementable event when a controller with a player state leaves the game well a controller with a player state that is essentially a player so here we have a reference also of the exiting controller other engine names it a bit different here we have a player controller object here we have a player controller and here we just have a controller object reference okay so when a player logs out i also have a reference of who logs out and then i can do something with that player what is something like this useful for well for one updating this array so that we have an accurate array of the players that are in my session so over here we can now type remove and i want to remove an item so let's say we have this array i want to remove a disconnecting player so this guy needs to go in here well as you see since this is the controller reference and this is player controller so this is also player controller reference this cannot connect so the first thing you're gonna have to do here is that from this exiting you're gonna have to cast and you're gonna have to cast to the controller that's exiting and as we see here for this whole gamemode setup we have the bp pc base setup so over here what we're going to do is we're going to do a simple cast to bp and then we need this blueprint player controller based setup and this guy is the kind that we want to remove from this ui over here so what you could also do as you see this is only a player controller reference you could also have this be a direct reference of the player controller that you use inside of your game mode so it could also be a direct reference of this bppc base setup and then you do not have to cast every time that you use this so let's say i do it for each loop with this variable over here now i want to execute an event that exists on that player controller then first i have to actually cast so the first thing i'm going to have to do if i want to communicate with each of these players player controller and call an event there like let's say pop up some ui on their screens for example then i'm gonna have to cast now as you can imagine if i put this cast in between here and basically straight away from the connecting player controller do the cast and then basically store that as a reference in here and don't have this be of the type player controller but have to be directly of the type bp pc base setup this one and then this object reference as an array so you still have to make an array out of it then i do not have to do this cast here anymore because of that point the cast is already made over here but that's just a matter of optimization so for now i'm going to keep it like this and on the event logout i can simply do the cast over here and remove that guy from my player controller so what did we realize here well as we see here we have a disconnection so let's call it event for when a player disconnects right that's what we have over here and what do we have over here events for when a player connects so what do we realize now let's remove this so what do we realize now inside of our multiplayer game well we realized player connection slash disconnection if i can type correctly so like this let's make this darker because that looks cool so now we realized in our multiplayer game player connection and player disconnection and we have this array okay so that's great this now exists on the server this variable i can set it to replicate but that's not gonna do anything because as i explained in the last video and if you haven't watched it please watch that so you understand this the game mode only exists on the server and because it only exists on the server i cannot replicate this variable down to clients so inside of anything that you code inside of your game mode setting a variable to replicate doesn't make any sense because it only exists on the server so who is it going to replicate that to nobody it cannot replicate only classes like a game state player controller character can replicate actually like everything else except for the game mode and the widgets can replicate basically so um okay now we manage this now what is next let's go back to this screen here so now inside of the game mode we have some connection setup that's the first thing that we require then let's take a look at the game state because i told you guys in the game state we can also have information about all of the connected players and that's true just like in the game mode we have specific events that only exist here in the game state we have that too but in the form of variables if you right click over here you can then type in game state and then you can find stuff that is specific to the game state and game state only so for instance if we scroll down over here we can see variables that exist inside of this game state so we can get here a copy of the game mode for instance we can just pull here we can get elapsed time that's a variable that's available for us we can get the gamemode class that we currently have we can get the match state so inside of the gamemode we can have a state of the match you can also code this yourself or you can use the the built in on wrench and stuff we can have this very nice variable called get player array and then these two other variables but the most important one here useful one here is this get player array so let's click on it and let's hover over it so as it says here read the value of variable player array player array array of all player states maintained both on the server and clients player states are always relevant and this variable here we cannot set anything on it it's just a variable that we can get information from so what does this mean well as we saw over here players connect and stuff and then we store a copy of them inside of our self-made variable so that we can basically have this variable and have control over these players but over here we already get a variable by default which contains our player states so every time a player basically connects to our game then they log into the game and at that point they get for instance a player controller as we saw in the game mode but players that we have in our game also have a player state and this game state automatically keeps track of all these connected player states so why is that useful well now we don't only have to use the game mode anymore to keep track of all of these players because the game mode can keep track of these player controllers but the game's state can also keep track of all of these player states so how does that what does it look like why is that useful well from this array for example just like from this array we can call those player controllers and do something there we can also just use this one and get the player state from the player state gap to player controllers so what does that look like if i do it for each loop through these connected players here and this array automatically gets updated by the way if people connect or disconnect this one automatically gets updated and we do not have to make our own code like this because this is our own custom variable we don't have to do that here this automatically happens so what's useful in this one well here we go the player state object reference so what we can do for example is we can do get player controller and from this player controller that is connected to this player state we can then do cast to bp pc and then not framework but bp pc base setup which is this tutorial so now in the game state we can also loop through all of this and call functions on every single player for example so yeah this is the same we can do it here or we can do it here and on both of these classes we can have an array and an insight and overview of all of our connected players all right so if you talk about something like for instance a player list then you could fetch that information of the player list from something like a game mode or you could fetch that information from a player list from something like a game state all right i think that's a good base setup that we need to discuss here this video is already getting quite long um i think i'm gonna round this video up now guys and in the next video i'm gonna walk you guys through uh now that we discuss basically this connection process let's talk in the next video about how we can actually initialize our player data so let's say a player connects to a match that player has information which is for example saved in a safe game such as his username his profile is avatar and such then how can we basically connect a player have to player fetch that information after player put certain information inside of the player states that it's replicated to other players and how can a player then initialize his own user interface to display his own information in and eventually how can the player spawn his own character because as you saw we put this default pawn class here to none so we don't spawn the player anymore so now we want to do that with code so how does that work well let's talk about that in the next video because this one is getting quite long alright guys have a good day bye [Music] you
Info
Channel: Kekdot
Views: 23,320
Rating: undefined out of 5
Keywords: Blueprints, Unreal Engine 5, Unreal Engine 4, Steam, Multiplayer, Game, Marketplace, Lobby, Game development, Unreal Engine, UE, Multiplayer game, Host game, Sessions, Advanced, Tutorial, Server browser, Sessions Steam, Ready up system, Online, Multiplayer Unreal, Replication, Replicated, MMO, Subsystem, Game Engine, Solution, Modular, Devlog, Listen server, Dedicated Server, Seamless travel, GameMode, GameState, PlayerState, PlayerController, Widgets, RPC's, Steam advanced session, Plugin, Series
Id: pvDgmnxewuk
Channel Id: undefined
Length: 22min 54sec (1374 seconds)
Published: Tue Jul 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.