How to Spawn in Multiplayer | Spawning in Unreal Engine | UE5 Multiplayer Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys quick interruption here before we start the video i want to thank our 14 patreons that we have already if you guys like our videos please consider supporting us for patreon it helps the creation of these videos so i want to do a quick shout out to norm dr robot preston linderman ariel kalinowski nippon seth chris anderson michael longer mace craig tsar blimpin ain't easy brian blackford rosie and craig smith thank you guys for supporting us you guys help realize these videos and enjoy the tutorial now guys hello guys and welcome to my new tutorial okay so in the last video we talked about how to set up a basic multiplayer setup how you can then assign your game mode to your level and your game mode contains all of the other classes that define your logic for your game and then the last video was about how players actually connect in a multiplayer game so we looked into the game mode and inside of the game mode we looked at the on post login event the on swap player controller login which means if you go from one level to another level while still connected in the same session then this note got called and then we looked at event on log out for if a player disconnects and then what we did is that we basically we stored a reference of all the connected player controllers so when a player connected here with a playercontroller object reference then we basically added that to an array of all player controllers on the gamemode so that the gamemode has an overview of all of the connected players so that's basically what we did in the last video and then i explained that to you guys and now we are going to continue so the first thing that you need to do in a multiplayer game is obviously you want to spawn a character and for this tutorial series i got a folder here called template with the third person character in here so in here we have this blueprint here for third person character and the first thing that we want to do now is we want to actually spawn the player and in this video i'm going to show you how you can best handle that now in the upcoming videos i want to talk to you guys about the next things so typically in a video game you have data that belongs to the player so it can be data that is meant for the characters so data that exists on this character like his health his strength things like that but obviously you also just have data of the user that is connected to the multiplayer session and i would like to call that user data so in this video i'm simply going to go through how you spawn your character but then in the next video i want to talk about how we can handle that data for the player so how we can assign certain data for example to the character that the player owns or how we can show some user data in for example user interface so as you see right now we only have a game mode with game state player controller and a player state but what i'm going to add is some widget classes and such that will also start up so the first thing in multiplayer let's spawn a character first and then after that in the next video it will be i will show you how you can then initialize your user interface and set your user interface up in multiplayer to fetch the correct data that belongs to a user okay so let's get this started so first things first most of you will probably know this section over here it's called world settings if you don't have that panel you can click here on window and then over here you can click on the world settings panel and inside of this panel you describe the game mode for the level and the game mode has the classes that define all of the logic for your game so typically what what uh beginners would do in our engine i guess is that uh you have a default pawn class here and you would simply set this to something like third person character and then at the moment that you simply hit play so i do play in editor then you would just spawn with the character right and then what also works is that if you do two players here and do a listen server which means one player is the host and the other one is the client so then we see that the client is here on the left and it walks around and it's fine and the server is here on the right which is also walking around and works fine but then there is an issue and that is that let's say that the character dies which is very typical in video games then well all we did here is to define the default pawn class and the character cannot spawn again so because of that reason we do not want to use this default pawn class so i recommend always setting it to none because we want to with code ourselves define when the character actually gets spawned we want to be able to control that for multiple reasons first of all you might not want to directly spawn with your character when you enter a game world you might want to have a countdown or something that actually spawns the character and the second reason is of course that obvious breeze and one of your character dies how do you handle respawning so what i recommend is in the game mode which only exists on the server i recommend that over there we code some stuff so that the player can actually spawn and find some player starts etc and then what i recommend is that the player controller which is you as the player that connects to the session i recommend that this player controller is the one actually calling those functions that exist on the game mode to spawn the character so i do not recommend actually coding all of the logic for spawning a character inside of the player controller in a multiplayer environment i recommend doing that inside of the game mode so since the game mode only exists on the server i recommend simply coding this type of logic here and then you also have a central place from where you can spawn characters so you might not always want to spawn a character that also has to be possessed for example so yeah i think the better place is to code this type of logic on the gamemode because then you just have a general place that anyone could communicate to instead of having it specific to the player controller which would be specific to certain players so let's call it on the game mode okay so the first thing that we're going to do is well we want to spawn this character called bp third person character which is the template character and to spawn in our engine you need to spawn at a location so the first thing that we're going to do is if we click over here and then go to basic and you can drag in a player start and now what we are going to code is that we want to find the player start on which the character can spawn so if we go over to the game mode let's go ahead and create a function a function is pretty much an event so so most of you will probably type a custom event and then you would make an event something like a spawn player and then you would code your logic here but eventually your event graph will get so big that i recommend simply creating a function so it's nice in this panel and then you can just call that function on the server so let's go ahead and first of all create a function and let's call it find random player start so i used underscores because that's basically how i define things so i want to find what do i want to find a random what kind of random a player start and then i'm going to assign a category to this and i'm going to call this spawning so first of all what are we going to code here well random player starts what we want to find so we're going to type in get all actors of class and the actor that we want to find in this example is the actor called player start so here we go player start okay so what is this note going to do well this note is going to find all of the player starts that it can find inside of the level and it's going to return it to us here as an actor object array so this is an array so all of the actors will be in here so and then the first thing that we'll do is that well we want to know how many there are so we're gonna type in length here we go and this will return the amount of player started find so in this case it will return four and then what we want to do is that we want to get a random one of those four that we want to spawn on so we're going to type in random in in range here we go and then the range is well the maximum is four and over here if the minimum is zero then this would mean that we now have five options and i can show you that like this if you don't understand that if i have a with zero is the minimum one and it ends at let's say four then these are five options one two three four five so what we need to do in order to correct this logic is subtract this by one to get that zero count and then basically what we do here is that we we put in zero over here and then we get this one over here so what did i just do well because this is a four then we basically have zero to three which also gives us four look one two three four okay so we want to get a random one like that and then that random one is that we want to spawn at so how do you do that well then you get a copy so if you simply type in get you can get a copy and you can get a copy at a certain index over here so if i plug in an array we will get a random integer out of the length so let's say it will return 2 and then we just simply get copy 2 so now we have random copy 2. and then this is the guy that we want to spawn at so from here we're going to type in get actor location and this location is basically where we want to spawn now what you could also do is get actor transform and then you also get the rotation and the skill so let's actually get that one and this is what we want to return out of this function instead of actually getting the player start here as an as an object reference we actually just want to get the transform of the player start because that's where i want to spawn so now if we select the input node here and click on output then we can get this to the end click this click this output away that it gives you here then we go ahead and connect it select this one hit q and then what we want to return is the transform and this we call player start transform alright so now we have this beautiful function and it returns us a random player start now uh if i fetch this function it looks like this so it executes so the function basically has to get executed like this but what do i rather have is that this function is just a pure function so if we hit on pure then basically it just simply returns it without having to be executed all right so now that we got this function we can find any random player starts transform in our level so that's beautiful let's close this let's hit compile and save now we go on to the next function which is let's call it spawn player okay so we want to spawn a player so what do we do first well a player might have a character that he or she already possesses so first of all we want to destroy that character because let's say that you just want to respawn somewhere it's just good to have some logic that you at least make sure that you destroy the actor that the that the player might already have so the first thing that we need is that we need to know uh who we are going to be spawning a character for so we need a player controller reference so we go ahead type in player controller then select object reference and let's call this the player controller okay so this is the player controller that we on the spawn for now the first thing that i want to do is that we want to check if it already possesses a character so we can do get a controlled pawn so this is the pawn that the character controls and then we can check is it valid so that means do we actually control upon and then if we do control pawn then we are going to destroy the actor so if i already post as a character then let's destroy that character that i possess and if i don't then that's uh let's just continue basically because then we can just go ahead and spawn my character directly then the next thing that we want to do is that we want to spawn an actor from class and the actor that we want to spawn can be defined over here so that's the bp third person character boom and then you see what it asks for so it asks for a spawn transform and here we go we got this beautiful function with a spawn transform so this is where we want to spawn and then keep in mind we also want to do this right if we did not have a character yet so this one we also plug it in um okay let's make this a bit nicer like this beautiful so what do we do now if we possess a character we destroy that guy if we don't we just continue and then we spawn a character here on a random player start then the next thing that we want to do is if you spawn a character it just exists in the world but nobody owns it no player controller can control it so for that we need to do something which our engine calls post post-acid so if we type in here possess and turn this off the context sensitive then you can see here the poses note so the character that gets spawned which is this return value needs to be possessed by a controller and who is that controller ta-da that is our player controller over here so let's rewire this a bit because this is a bit long like this and then let's do it like this all right perfect so the player controller that basically calls the spawn character function then possesses this character and there you go so now this works and now we can go ahead and test this out now there's two ways that you can go and implement this first of all what you could do is on event post login you could simply call spawn a player and you could say okay spawn a player for this incoming player that would work but then it would only work just the same as this works where you just basically assign a character here and it just spawns on the beginning of the match and then that's it but we don't want that we want the player controller to spawn it when the player controller wants to spawn it so what we do in this example is that we go into the player controller and we want to spawn it on begin play now you have to keep something in mind because since the player controller as i explained this on previous videos in this series the player controller exists on the server and on the owning client and that means that this begin play will actually fire twice and you will now see that if i type in print string it will say hello if i then compile and save and play with one player in standalone and click on play then you will see that it only spawns hello so it only says hello but if i spawn here as the server and go play it will also say hello but then if i spawn two players then the client will say hello twice so now we see hello hello hello and that means that well the server obviously only exists on the server and is the server itself but the client over here spawns so the server says hello and the client says hello and then on the client that says hello so that's basically what i mean with and what i explained in the previous videos the player controller exists on the server and on the client so what happens here the server sponsor player controller and fires this became play once for itself so that's fine that guy could now spawn a character for itself using this spawn player function let's actually assign this to the spawning category and the client could do that as well but be careful because since it exists on the server and on the client as we just saw it will spawn it will call the function twice if we are not careful so how do we handle this one well we only want to spawn this function from the local player controller and not the one that exists on the server so for the server the local player controller will automatically be this first hello the server itself and for the client it will be client one that says hello and that middle hello that that one is basically being ignored in this scenario so how do we do that we can type here if we right click we can type in is local player controller and this will then as it says returns rather this controller is locally being player controlled so right now if we do the branch and plug this in over here and go off of the true then you will only see that the server says hello once and that client1 says hello once so now we have two hellos which is what we're looking for since we only have two players so right now we see server hello and there we go client says hello so it only shows two messages now let's make this p in the screen a bit longer so hello and hello and that that's basically how we can make sure that this doesn't trigger twice and if you guys ever were confused about why triggers twice that is why okay so what do we want to do now that we know that this only gets fired once per player basically and the server is also player now we want to spawn the player so how do we do that we do get game mode and then the game mode that we want to get is the blueprint gamemode base setup it's this one over here so from here we're going to cast to blueprint gamemode base setup and we're going to plug this in sorry guys if i explain everything in too much detail but i just want to be thorough with these tutorials you know okay so the gamemode here and then what we want to call in the game mode is spawn a player so now we can go ahead and we can call spawn player and then as you see it asks for a player controller well we are the player controller so if we type in self then we spawn a player for ourselves so what will happen right now if i hit play let's test it out if we hit play right now this guy on the right which is the server gets a character and this gun left is stuck in the floor on the zero zero zero position does not get a character why is that well that's because of my previous lesson the game mode only exists on the server and since this is now being called by client one like the client one is not the server so cannot directly call gamemode events that exist on the server so what we have to do here is very simple we simply have to make this sure that we call this event on the server so if we right click and type in custom event and call this sr which means server spawn layer then we can make sure that this event is run on the server and is reliable and this event we wanted to call this logic over here so let's do it like this and then we want to call this event so as our spawn player and now we will see that the server itself will call this and spawn a player for itself and client1 will call this and spawn a player for itself so let's put it like this and now it will work so if we hit play boom and boom so now i can walk around let's make it bigger oh sorry so now i can walk around with this guy which is the client and i can walk around with this guy which is the server and there you go that's how you can have controlled spawning now let's test one more thing let's do keyboard p so button p on my keyboard if i press that what i want to do is i want to spawn the character again in case of respawning so how do we do that well i simply want to call sr spawnplayer for respawning the character and then we can check the logic that we made here that if i already possess a character it gets destroyed so let's do that if i now go ahead and hit play then i'm here on the left with the client i can walk around and then i go stand over here now in case that i die i my character will have the whole event of dying of course we'll lay on the floor and stuff and it will show in the user interface typically there's then a countdown and then after the countdown button p gets pressed then the character gets respawned so now i properly returned to the player start and that guy that was over here is gone since it is destroyed so now we have achieved proper multiplayer connecting basically from the previous video and then in this video we achieved the ability to spawn a player with code which we can basically dictate ourselves right now in the game when that stuff happens so if i press p this guy respawns and then if i press p this guy respawns as well that's it for this tutorial of how to spawn the player alright guys in the next video we are going to be talking about what i previously talked about in the beginning of this video which is basically now that we know how to spawn a player let's start by initializing user interface and let's have that user interface properly display information about the user so it could be a username it could also be some stats that belong to the player here so basically how do you properly handle creating user interface in a multiplayer scenario and making sure that you do not have any invalid stuff and that your data is always properly pulled at the right time after every class in multiplayer is properly initialized alright guys i hope you enjoyed this video if you did be sure to give it a like and subscribe that helps out the channel and also consider checking out our patreon since that helps us to be able to continue these types of videos okay guys have a good day bye [Music] you
Info
Channel: Kekdot
Views: 23,675
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, GameMode, GameState, PlayerState, PlayerController, Widgets, RPC's, Spawning, Respawning, Spawn, Player Start, Player, Blueprint
Id: H0JZnWdY0k8
Channel Id: undefined
Length: 21min 35sec (1295 seconds)
Published: Sat Aug 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.