Unreal Engine Multiplayer Framework | What to program where? | Multiplayer Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys quickly before we get into the video i want to thank our monthly subscribers greg smith with the twenty dollars davey with the ten dollars and missus mode 3d with the three dollars thank you guys we appreciate you and if you guys also want to support us check out our patreon link in the description and guys we want to thank you guys for reaching 5 000 subscribers we just hit it yesterday and we are really happy and excited about this so thank you guys for all the support and let's go to 10 000 subscribers okay bye-bye hello guys and welcome to this tutorial about the unreal engine multiplayer framework basics so basically in the last videos the first video in this series actually we talked about what sessions are and how players can basically host matches and then other players how they can find those hosted matches and join them so here we see how somebody can create a session then he becomes online and available for other people to join and then the other person can basically click on find sessions find that session and then join that player in multiplayer so what we learned in this first video is basically how people can connect to each other in multiplayer games then in our second video we took a look at replication basics so i've set up this little map over here and in this video we talked about what replication is so we looked at the term replication how you can have an original and then replicate the original then in this video we also took a look at an actual example of how replication applies so how you have a server instance and how that server instance then basically replicates to the clients to keep them all in sync with the server state then once we understood the concept of replication we also took a quick look in this video about how replication is actually applied in our engine so we took a look at how we can turn replication on we took a look at replicated variables and wrap notifies and we took a look at what remote procedure calls are rpcs in order to have the client communicate back to the server and also to have the server broadcast information to all of the clients so now that we have a good understanding of all of these basics for multiplayer how we can connect to each other and overall the replication concept in our engine i want to take you guys through the framework and the most important classes when it comes to unreal engine multiplayer so let's hop right in that alright so the first thing basically when you start a level in unreal engine you see that here on the right we have a world settings tab and if you do not have this tab then you can click here on window and then over here you can open up the world settings and what we see in that tab is that basically here a game mode is defined and what we see is that a game mode contains all of our other classes so there's classes in here like a default pawn class a class like a player controller game state player state so what are these classes and why are they relevant and what should we basically code where because i can for example trigger that widgets will start on my pawn class and then be created but i can also do that in the player controller so what to code where and how do these classes relate to each other is what we're basically going to take a look at for this video so the first thing that i want to do is basically create a folder over here and let's call it core and inside of core we are going to be creating the essential classes for multiplayer so the first one that's always important if we right click here in the content browser and click on blueprint class is our gamemode class so quickly what is a gamemode well as it has over here a gamemode defines the game being played the rules of the game you can also keep track of things such as scoring and other facets of the game type and in reality when it comes to rules and scoring for example you would only use the game mode to actually award score so you would only program basically the rules of your game instead of actually keeping track of what the score is here so the first thing we have to do is create a game mode but there is two types of game modes in our engine so there is a gamemode base class but if we click here on all classes and type in game mode then we see that in this hierarchy over here we have gamemode base but we also have a gamemode class and the difference between these two is in the beginning there only existed a gamemode class and the gamemode class was actually intended for multiplayer match based games it also says so when you hover over this behaves like a multiplayer match based game it has default behavior for picking spawn points and match state basically inside of this gamemode class we have a couple of events which we do not have inside of this gamemode base class when our engines started off they started off as a game engine for fps games their own game called unreal tournament and this game mode class was really made for an fps type game like that so it has some match state events in there that you can actually call inside of the event graph now later on when they started to see that other engine also became more popular for single-player games they also added this gamemode base class and the gamemode base class is the one they offer you here by default but for multiplayer i always recommend using the game mode class because if you do want to use some of these handy events or functions that you can override then i recommend this class also one thing to keep in mind when we type in the game state class then for the game state we also see that we have a game state base class like this and then over here we also see that we have a game state class if right now we start to type game mode and select the game mode instead of the game mode base then we must also create a game state class and not a game state based class if you combine a game mode with a game state base class for example your game will break so once you go down to not using the base classes but the actual game state and game mode classes then you have to use both of them alright so the first thing we're going to do is type in gamemode and create ourselves a gamemode now we are going to call this one bp which stands for blueprint gamemode and let's just call it in this one framework since my category here for this tutorial i call this framework so i'm just going to add an acronym or framework on the end of all of these okay then the next class we're going to create is going to be that gamestate class so let's type in gamestate okay so let me explain to you a little bit about what a gamestate is but essentially as the name already suggests it keeps track of the state of your game a good thing to know is that the game state is also replicated so any information that you set here on the server gets replicated down to every single client so let's create one of these as well call this blueprint gs which stands for game state and let's call it framework as well then the next class we're going to create is called a player controller you can find that one over here for the player controller there's no player controller base class or anything like that so it's just a player controller a player controller is basically you as the keyboard controlling your player whether that's a pawn a car or an actual humanoid player this player controller actually represents you and the input that you do in your game so if you click on your keyboard or on your mouse for instance the player controller represents that so as it says here in our engine's own description a player controller is an actor responsible for controlling a pawn used by the player okay let's create one of these and let's call it blueprint underscore pc and then framework all right so as they said in their own description it's responsible for controlling a pawn well that is true but a player controller can also handle a lot more and one very important thing for a player controller is that it's a very useful place to spawn all of your user interface from and also to manage all of your user interface from and i'll show you guys in a minute why that is and i'll show you guys in a minute when we get into this class why that is so alright then the next class that we want to create is back to blueprint class here is a player state class so let's hover over it and read what it says again a player state is created for every player on a server player states are replicated to all clients and contain network relevant information about the player such as the player name score etc what this means is that by default this class has a couple of functions and a couple of variables and one of those variables as they display here is for instance the player name or the score of a player so basically this keeps track of the state so you would save things here such as for instance your username perhaps you would save other replicated information here such as for instance your guild id perhaps you want to show your experience or the level that you are as a player or perhaps you want to show something like a player title like you could be cactald the epic adventurer so let's hit player state and hit select and call this bpps which stands for player state framework right then the next thing we're going to do is that we want to have a one user interface class and we click on user widget and let's call this wp which stands for widget blueprint framework example let's call it that all right so the first thing we're going to do is now hit save all and then what we need to do is that we need to assign this game mode to this level in order for this to work so here we click on blueprint gamemode framework and then we see that by default this framework is set up that it has a default pawn hood class player controller and stuff like that now we can add all of these classes here so the classes that we created over here we can add them over here but what you can also do is open up your game mode click on class defaults and then you can set them over here so whatever you prefer do it like that so the first thing we're going to assign here is gamestate class so for that one we're going to select our bpgs framework then we're going to go to the playercontroller class here we click on playercontroller bppc framework and then the player status the last one we have to assign bpps framework all right now let's hit compile and save save all and as you see they are now assigned to this gamemode so what happens right now when i hit play well i just become some flying thing and this flying thing that i am right now is actually this default pawn that our engine has given us so the first thing i want to do is switch this out to a character so we have a character here called blueprint third person character it's just from the third person template and right now if i hit play i actually spawn this guy all right now that we have this base setup out of the way i want to explain to you how this framework works and where each of these classes exist so does this one exist on the server for instance does it also replicate to the client let's take a look at that right now so i made this little actor here and inside of this actor in the event graph we have some information about where these classes exist so let's first of all look at the game mode since the gamemode is the very first class that gets initialized and sets all of the other relevant classes okay so basically as we know game mode handles rules and stuff like that so where does this guy exist well it exists on the server only so what does that mean well that means that the clients do not even have a copy of the game mode so the clients cannot get any information from the game mode and they can also not communicate with the gamemode directly they can if they do rpcs but then they have to make an rpc which they fire off to the server first and then once the client is on the server they can at that point basically communicate with the game mode so let's say that in the game mode you install an event that is called a respawn player what you can then do is that from your player controller which as we see exists on the server and the owning client then what you can do is that from the owning client of the player controller and to understand what owning client means i wrote it down over here owning client is the player who owns the actor so basically when you spawn into the world you own a player controller and you own that on your local computer so you have over here you have you on your pc basically let's make this a bit smaller but then your player controller your exact one also exists on the server so your pc on the server so there's two versions of your player controller basically that's what pc stands for there's the one that you have locally and there's the one that exists on the server that is always correcting the one that you have locally in case you are replicating information down from this player controller to this one okay so let's say that the player controller wants to respawn their player and that and that respawn events exist on the game mode well in that case first we have to go from the owning client locally to the server and then once on the server we can then call the gamemode and activate an event there so to look at that in action we can type in custom event and we can call this respawn player okay so let's say that this event exists on my player controller then what we can for instance do is that we can do keyboard button p for instance so let's say i want to respawn my player on my player controller i can then on my keyboard press p and i can then call the respawn player event well since this is just a regular event and it's not replicated this will just happen on the owning client but on the local version of my player controller so now that we want to do the respawn and the actual logic is here save server-side only on the gamemode we have to go to the server first so in this case we would now create another event and we would call this sr which stands for server respawn player and this event we would then set to run on the server and we would make it reliable since this is an important event that we need to make sure actually comes through the network so in that case we can then press p to call the respawn player event and a reason for player event can then call a respawn player server event that exists and what we do right now is that basically we were on the owning client side over here and at this point over here we breached that gap from going from the owning client version of our player controller to the server side version of our player controller so we went from owning client to server and now that we are on the server and since the game mode only exists on the server we can now communicate with that gamemode and call an event there so let's say that over here we have an event called spawn player right and that event we do not have to set to run on the server because as you see over here everything that happens on the gamemode is always run on the server so on the server you do not have to use any of that run on server stuff so what we can do right now is that over here you could now basically get your game mode what you would then typically do is that you would cast to your game mode so you would cast to and find your gamemode class so this one and then on your gamemode that is what this reference is you will then call that spawn player event but since we are now in an actor class blueprint all of this is inside of an actor class blueprint i cannot actually make this work but this is just an example of how that would work so if you would do that cast so let's bring it back actually if you would do that cast then it would look something like this spawn player boom so basically this logic would run through the cast and then on the cast you would spawn that player inside of that game mode alright so now that we understand that then let's look at the following class let me move this over here a little bit so we have a bit more space so this is all inside of your player controller okay now that we know that the game mode only exists on the server let's look at the game state so the game state as we know it had information about the state of our game so what can that be for example well the state of our game could for instance in a team-based game mean that we have a score of team a and that we for instance have a score of team b and as we see over here on the top the game state exists both on the server and on the clients so that would mean if we set this type of information such as a team score let's say that the team score is 5 for team a and 2 for team b then this gamestate class can replicate that information down to all of the clients and that means that we can now have a global place where we can store this type of information and have that global place with all that information basically be available for the clients to fetch their information from so what would something like that look like in actual code well let's move this down a little bit it would simply look like this anywhere where you are a client so that could be in your player controller or in your widget because your widget is owned by your player controller i'll get to that in a little bit then basically what that means is that on any of these classes such as for instance the player controller let's say that i all my player controller would want to pull this team score so that from my player controller i can then set that information in my widget or let's say that for my widget i would want to get that team score then i could basically from my widget make a call to the game state to get the information there so let's say i want to display in my widget i want to display team a score now what i can do in my widget is run this code where i can type get game state and then we would also have to cast to the blueprint game state casting is basically making a phone call to another blueprint and then we can fetch all the information that's available in the blueprint so if this variable exists in the game state we will now be able to get the team score so what that would look like is that this information here would basically come out of here and now our widget could be updated locally with the information from that game state alright now that we kind of understand what the game state is then let's take a look at the player state so as we saw before and as the name indicates this class keeps track of the state of the player and also this class exists both on the server and on the client so basically what is this useful for well as we read before this could for instance contain your username let's say that your username you want to have it replicated you would then put that username over here so why would you not store your username in your playercontroller well because as we see over here playercontroller only exists on the server and owning client and the owning client is basically you the guy or girl behind the keyboard and mouse so that means if the player controller only exists on the server and on the owning client then what about all of the other connected clients that would also want to know your username well there we already see the problem with the playercontroller and because of that we want to store this type of information not here but in the player state where it's supposed to be alright now that we understand why the player state is useful then let's look at the pawn class and one of the most famous pawn classes is what we call the character class so that is basically this guy that you can walk around with so usually in video games a character might have health and your help might go down and your character might die and have to respawn so if we have a variable called health then where do we store that well well you could for example store it on your player controller again but then we have the same problem as with the username the other clients will never know about your health and if the other clients don't know about your health then how can they base logic on your health and see that you have for instance only 10 hp left well because of that we do not want to install this yet again in the player controller but we want to install it in a class that exists both on the server and the client and what you would preferably want is that when your character dies that your health variable dies with the character so that basically answers the question why you do not install your health on your player state because if you install your health on your player state then in the scenario that your character dies you would have to then reset your health so a more clean way to do it is to install the health where it belongs with the character have it die with the character and then once the character respawns have that held variable automatically be reset because it has a default value that you simply set inside of that variable all right now that we understand the pawn class as well we looked at most of the framework and most of the important classes here then another good thing to know is that there is a relationship between the player controller and the pawn class and so you could basically draw a line here between the player controller and the pawn class and the relationship is the following let's get this out of this example and copy it over here all right the relationship between these two is that the player controller is basically above the character class and also input wise so let's say that you have your keyboard over here right and that you press buttons on your keyboard and then let's say the same for the pawn class let's say that for the pawn class you also have some buttons that you can press so let's get one of those buttons let's do this one p so let's say here i can press p and over here i can also press p now how does this work basically well if p is also installed on the player controller and also on the pawn then the one on the player controller is the only one that gets consumed if there is no p event on the player controller then the pawn consumes it so there's a real relationship between the player controller and the pawn where first under engine checks if the player controller consumes the input and if it doesn't then the pawn will basically consume that input so that's very handy to know and why is that handy to know well let's say for example that here in the player controller we have a button and we want this button to spawn some user interface let's say p wants to spawn a pause menu well in that scenario you would then basically install p on the player controller and it would consume the input here first so now p is basically already gone for us and we can no longer use it in the character because every time we press it the player controller simply consumes it so that means on the character here we can no longer use that so we will have to assign some other buttons like violent d for instance so that's just one important thing to understand then another important thing to understand is that player controllers can possess pawns so essentially since the pawns are basically created on both the server and the client they just exist in the world so upon spawning of a character it just it just stands there and nobody owns it but then you have to have an owner that controls the pawn and that owner can then be you the player controller so as soon as this player controller possesses the pawn then the player controller becomes the owner of the pawn and at that point basically the pawn will consume the input that you give as the player controller but first it will check if it was used here and then finally your player controller input is then actually used here in your pawn class alright then you might have the following question why does the pawn class exist on clients and not on the owning client well as you can imagine if you were to have two players spawned as a listen server like this the whole goal of your pawn is that you can both see it on the left instance here on the client instance and on the right instance here so that's the reason why the pawn does not only exist on you as the client because if that were the case then it would actually look something like this this is a good example for basically only having something on the owning client so now i have my character here and in this instance here on the left i have my character here but they do not see each other because this is now testing a standalone which means i only see my character my owning client and this guy only sees his character on his only client so that is basically why a character exists on the server and on the client all right now let's talk about the very last example over here let's reset this and that is basically widgets so then how what what are widgets in this entire picture and how do widgets relate to these classes well widgets only exist on owning clients so just like the example i just showed you with that character only being visible to me in that scenario that's the same with widgets widgets are basically drawn on top of your screen and you only want them to be visible for the owning client and every connected owning client can have widgets so what do i recommend when it comes to widgets well i recommend that your player controller who is also existing on the owning client that your player controller sponsor widgets a good thing to know about widgets widgets can never be replicated or anything like that and that is of course again because they only exist on the owning client so having your player controller spawn and create all of your widgets and also managing them is a very good id because now your player controller can manage your own inclined widget and then you can have a relationship between the player controller sending information from the server down to the owning client then this owning client side of the player controller can send that information over to the widget and have it be displayed to you as the user and then let's say for example that you want to respawn your player right but you do not want to do it with the press of a button inside of your player controller no you want to actually handle that with an actual button inside of your user interface well in that scenario there's also a good relationship between these two and that is that let's say you have a button installed inside of your widget that you want to press well when you press it over here you have your owning client widget communicate to your owning client player controller and then on the owning client of the player controller instead of having this p event you could simply have the widget called this respawn event that exists in the owning client of your player controller and then from there you can have your player controller call an event on the server and your server side player controller can then call that event anywhere in these classes in the game state in the player state or in the game mode or in your pawn class for example so now your widget can communicate with the player controller and your player controller can then respawn your character alright guys i think this is quite a long video already so i'm going to wrap it up over here in the next video i'm going to take a closer look at the gamemode and what events are actually inside of there then we are also going to look at the game state and which events are inside of there at the player state at the pawn at the player controller and at the widgets and basically in the next video we are going to set each and every one of these classes up with some useful code and a really good example of how these classes are best used and i'll just simply show you some best practices of what to code here what i recommend you code here here and here alright guys i hope you enjoyed this video and i'll see you guys in the next one bye [Music] [Music] you
Info
Channel: Kekdot
Views: 43,798
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: Hsr6mbNKBLU
Channel Id: undefined
Length: 27min 44sec (1664 seconds)
Published: Tue Jun 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.