Learn Unreal Engine Networking in 30 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video sponsored by my course the Unreal Engine C++ survival calls using my knowledge from working in the industry we start from the basics and work our way up until we've created an online survival game with C++ we create vehicles clothing weapons steam matchmaking and much more get lifetime access to $25 using the link in the description today I'm going to be talking about Unreal Engine networking and if I do my job correctly in one hour you should have a excellent understanding of how to do networking in Unreal Engine and I'm gonna go into C++ at the end I'm gonna keep things more conceptual at the start because it's important you know what's going on before you actually learn how to write the code so what are we even doing when we network again well really what we're doing at the core of it is we're just syncing a bunch of different games together so here I've got four different players all playing the same game but it's important to know that I'm playing the game say in New Zealand someone else might be playing the game in Brazil someone might be playing the game in Japan we're all running different instances of the game and the whole point of networking is to sync up the games and make them look like they're all the same game so that we actually feel like we're playing together if you want to follow along in this video just go to content examples click for content examples and download them for 4.24 and you should be able to follow along in the same way that I do in this video I will also leave a link to the new work compendium by XE in the description it is a excellent resource it's literally a hundred and eleven pages of just information about Unreal Engine networking so you can get a lot of the finer more granular detail from this so I'll link that in the description as well so there are two types of service and Unreal Engine you have a dedicated server and you have a listen server a listen server is when one of the players in the game is the server you can see in the top left corner that guy is the server he's actually playing began a dedicated server is when the server is not someone playing the game the server is just a standalone server that hosts the game and everyone that connects to it as a client there's no server actually playing the game the next thing to touch on is that the server is the authority he controls the rules of the game and if he didn't control the rules of the game it would make the game way easier to cheat in so basically what happens is if a client wants to do something like shoot a weapon he shoots the weapon but then he tells the server hey I've shot my weapon at which point the server goes ok let's see if you've actually shot someone and if the server thinks you the guy then he will decide to deal damage to the guy so the clients don't talk to each other they talk to the server and then the server will replicate all that back to the clients in the game so if I want to shoot this guy over here I shoot I tell the server I've shot the server might validate things like he might check that I have enough ammo to actually do the shot and then if the server decides that I've hit the guy he'll deal the damage and then he'll replicate that back to me and that might mean that I see some blood particles appear it might mean that I see the guy die but the server is going to send information back to all the clients in the game just know that he is the true game now in an ideal world right we'd have the server here playing the game and all we would do is the server would send his exact game world to all the clients in the game and all the clients in the game would just display the server's game world exactly as the server sees it but unfortunately there is one way way too much information you would need to see in every you know position of the players what animations they're playing that is just way too much information to be sending over the network so with networking the big challenges replicating as little as possible to make the players feel like they're all playing in the same game without actually sending everything so the most simple type of replication of wrap our heads around as active replication it's literally a checkbox you check it and now that actor is replicated and I'll show you that in a second but what it means is if the server spawns something in all the clients will see it as well so for example imagine a rocket-launcher I shoot my my rocket launcher and I tell the server hey I've shot a rocket now if the rocket is not replicated none of the players are gonna see the rocket if the rocket is replicated all the players are going to see the rocket so for this ghost here replication is not turned on when the server spawns this ghost in none of the clients in the game see it but by replicating it over here all the clients can see this ghost because the server spawns it in and because it's replicated it will automatically show up for all of the clients in the game and I want to show you how simple it is here's the blueprint for the ghost that replicate and the only difference is this replicates checkbox here is not checked on the replicates version we have tricked the checkbox and that is literally all you need to do so if you check that box when this actor gets spawned and on the server all the clients will automatically see it so if you wanted a rocket to spawn in on the server and all the clients see it all you need to do is make sure this box is checked and that will happen so as I explained at the start the server's game is the true game and the clients are sort of just copying off the server's game and so let's have a look here at the server the server is updating the health of these two ghosts on the server this works perfectly fine you can see that the ghosts health are slowly going down by 10 units and both ghosts get updated on the client though the health is not going to work it is not getting updated on the client however if we enable variable replication now our health is updating perfectly and so this is why I love Unreal Engine let me show you how easy this is to get this health variable to sync here's my variable for the health and you can see this is the ghost that doesn't replicate it is broken the health value does not update you can see that the replication value is set to not all we have to do is click on the health variable and set it to be replicated and now when the server changes this health value all the clients in the game will also get that exact same health value and the value will sync up perfectly you'll notice the difference here in blueprints as well is that when we update the health we take 10 health away but you can see that there is not a little replication icon next to the health so if you see these little dots next to a variable it means that that variable is replicated so when a server updates this value all the clients in the game will see it the server needs to be the one updating this value the clients cannot update it remember the flow of data is the server does the update and then all the clients in the game will have that data replicated down to them now the whole point of networking is to keep all of the clients and sync with the server so you would very really want to do something like this but if you wanted to you could have something look different on the server to on the client and in blueprints all you have is use this note called has Authority authority means that I'm the server and remote means that I'm a client I am a remote machine that is connecting to the server and you can see here that on the remote machine we just set the ghosts to be a different value if we're not the server and so that's how you would achieve that functionality if you wanted to do it and again coming back to the flow of data we're going to be using this a lot because remember if something is replicated it is the server that needs to control it and then that data will be replicated down to clients the health for example we always update that on the server and then the health value will be replicated automatically down to clients so we would want to use this to make sure that we are the server before we do something like see a health value the remote would not set the health value because when the server change is that it's automatically going to be replicated back to the remote the client ok so let's talk about Reap notify now on the left the server is changing a value and what the value does is it updates this traffic lights here and it does it by updating the material on the traffic light to get it to update now this is a replicated value and the client is receiving that information but the clients visual of the traffic light is not updating yes it is receiving the data but it's not updating the traffic light so what a rip notify does is it's basically a way of saying when this value changes run a function and in this case the function we're running is going to update the traffic light so here's my traffic light you can see this is the value it's called the street light scalar this is the value that updates the traffic light and it is replicated so the information is being sent to the clients but the clients just aren't updating there are their own version of this traffic light so all we have to do is change the replication to be rip and notify what this will do is it'll give you a rip notify function you can see this is it here on rip streetlight scalar this function will be called on the clients that are connected to the game every time this is updated and the clients will then update the material they're doing a bit of custom logic here to update the street light material so now what's gonna happen is on the server and all the clients this function will get called and the traffic light should now update and if we have a look now the server is going to update that streetlight scalar value and when the client receives the new information it then calls that Amrit function and now what happens is it can update the traffic light and now the two games will stay and sync so if you've made it this far this is the core of how replication works an unreal engine variable replication and actor replication are the two big ones but the last part of the sort of whole system is something called our pcs you remember at the start of the video how I see it if the client wants to shoot his gun he needs to tell the server that he shot his gun well how does he tell the server that he shot his gun he uses a server RPC the server can also communicate back with the client using something called a client RPC so if the server decides hey you've hit that guy he can mean to a client RPC to make the client display a hitmarker on their screen so they know they've had someone so the client and the server can communicate back and forth using these are pcs there was also one last special type of RPC called a MIT multicast RPC this is an RPC that you can only call from the server and when you call it it will happen on all the clients that are connected to the game so if you wanted to make an explosion go off and everyone in the game see it the server can call a neat multicast function that makes an explosion happen and now all the clients in the game are going to see that explosion happen and the game is going to be synced up now this might currently be a little bit confusing because if you're the client why do you need to tell the server to do stuff we've kind of established that yeah you might want to tell the the server to shoot remember that the server is literally in control of the game so if we want to modify someone's health we need to tell the server to do it we never do it ourselves so if we're a client we always tell the server to do something so let's say an air game that we want the cupcake to be taken away if you press a certain key well on my screen I can take the cupcake away no worries but none of the other players in the game are going to see the cupcake get room moved from my hand the way we do it is we tell the server I want to remove the cupcake from my hand the server will then have a replicated variable and when it updates all the players will get that notification that the cupcake has been removed from my hand so let me show you how you would implement something like this inside of the character I'm going to add a variable and I'm going to call it has cupcake now the server is in control of this variable but I want everyone else in the game to know whether or not I have the cupcakes so I need to replicate this hAER'S cupcake variable and I want to do a rip notify because we know change this data I need to call the function to hide the cupcake so let's go ahead and do a rip notify this will automatically add the enroute function for me you can see here that the cupcake is the static mesh one so what I'm going to do is I'm going to take the static mesh one and I'm simply going to hide it so if we see it hidden in game to true this will hide the cupcake okay so let's do it so that when I press the X key on my keyboard it hides the cupcake so I'm just going to type in X and I'm going to find the X event for keyboard events so when I press the X key we're going to sit the hares cupcake value to false to hide my cupcake I'll make the default value for those true cuz by default we should have the cupcake so when I press X we are no longer going to have the cupcake okay let's try it out and we'll see if it works okay so you can see me in the game I'm gonna go ahead and press the X key on my keyboard and you can see that literally nothing happy and if you know a little bit about networking you'll know exactly why this happens remember the server is the authoritative game it needs to control the data and then feed that back to all the clients so we're gonna do our first server RPC when I press the X key I need to tell the server to do this and then the server is gonna do it and then it should call this function and it'll hide the cupcake so how do we do a server review I'll show you how to do this in C++ as well but for now we're sticking a blueprint I'm going to add a custom event and I'm gonna say heíd cupcake then where it says replicates we need to go to run on server and we'll make it reliable we're not going to worry too much about what that means for now just make sure that this event runs on the server and so now what we're going to do is we're going to plug it in here and when I press the X key what I want to do is I want to tell the server to hide the cupcake so all I have to do is call this event and it will automatically run it on the server the server then sets this value and everyone in the game is then gonna call that notify function which should hide the cupcake okay I'm here on the client I'm gonna go ahead and press the X key and as you can see it hit the cupcake and everyone's game is in sync you can see that on the screen the cupcake also disappeared that is how you do game play the client tells the server to change some value the server changes the value replicates that state back to all the clients in the game and that's how you would generally create a networked feature you might also be wondering why all the clients can see each other running around and that is because the default character movement and unreal is actually networked out of the box so you don't have to do anything to get networked movements it up and going in your game at this point you should have a pretty good understanding of unreal engine networking the very basics anyway so from here on out it's going to start to get a little bit more complex but I'll try to keep things as simple as I can so let's look at this hares cupcake variable so we have a rip notify which means women this value changes I want to call this function and do some stuff okay well let's look at the replication condition under that the replication condition kind of tells you how you should replicate this value and this is an optimization so we don't always need to replicate this value there might be times where we actually don't want to replicate a value to all the players in the game or maybe we just want to replicate it to some of the players in the game maybe this has cupcake value only ever changes once and if that's the case then we can use initial only as an optimization the game will still work perfectly but it will only seem this change once and then it will stop replicating this has cupcake variable so replication conditions a way to optimize your networking in your gap which is really important if you're trying to make like a better real game where you have maybe 50 players on one map okay next up we're going to talk about something called a net multicast event a new multicast event is so tempting from a beginners point of view and beginners fall into the trap of using this everywhere you should not be using this very often it was useful in some cases but people definitely overdo it and it can lead to a lot of bugs in your game if you use this incorrectly so a tempting thing that you can do is on the server you can run a neat multicast event and what that does is it will hide the cupcake because it's an event that happens on all the clients just like my rip notify but there are differences and we're going to talk about these differences soon so let's add another event I'm gonna call this fired cupcake multicast it doesn't really matter what we call it but just make sure that it is a knit multicast and then what I'm gonna do is I'm not going to do this notify thing anymore I'm literally just going to say hide cupcake multicast so on the client I press X I tell the server I want to hide the cupcake the server then does this function and when the server does this everyone in the game is going to run this so it's a little bit complicated to wrap your head around but if we come to the hide cupcake multicast you'll see exactly how this works when I press X the server is going to make everyone in the game do this they're going to hide the cupcake and look how easier this is I press X and the cupcake is hidden for everyone in the game this is so much easier right I don't have to worry about a variable and this on-ramp this looks very tempting to do it this way instead but you shouldn't always do it this way here are the problems the multicast is only gonna happen if you're in the game so if someone joins the game in ten minutes after I've done this they're going to see my player holding the cupcake and this is a problem we also don't seem network updates all the time if you're on the other side of the map I'm probably not going to seem network updates to you you can control this it's an optimization so if you're on the other side of the map and I do this multicast you're not going to run this multicast if you're on the other side of the map because we're optimizing and so if you run to the other side of the map this function never got cold and on your screen it's going to look like you're still holding the cupcake right the games are going to get out of sync and this is exactly what we're trying to avoid so coming back to the old way of doing it we tell the server to hide the cupcake the server is going to change this variable and then it calls an honor it function this fixes the case if a player joins halfway through the game or ten minutes after I've had the cupcake this function still gets called because the player is going to join in the game it is going to see that this value has been updated and it's going to call this good as gold so if you join ten minutes after we hide the cupcake it'll still work perfectly fine if you're on the other side of the map and you run over to me I will not be holding the cupcake everything will work as expected so let me give you a better example of this because unreal provides an example of us in the content examples so here's the first instance we have this chest it doesn't have any replication so when I walk up to the chest on the server you can see that the server is going to open the lid but for everyone else in the game we don't see the lid of the chest opening now remember when I see it if someone's on the other side of the map they're not going to see what happens well obviously I can't show you an example of that but this ring we're pretending that being outside of the Ring means you're on the other side of the map this is called relevancy if you're too far away from something you're not going to receive the network information about it so let me show you an example of this one of the clients is going to run up to the chest we're using a neat multicast function so you can see the client and the server open the chest but the people that were outside of that ring they don't see the Chiefs to open at all even even if they run up to the chest they're not going to see it open because remember the neat multicast only gets called if we were sending Network information at the time so if all the clients are standing inside of that ring it's actually going to work fine because they're all receiving network information so the chest works just fine so in this example the next one we use a rip notify instead of a net multicast so this should fix our problem right even if you're outside of this ring or you join midway through the game it should work just fine let's have a look so you can see that the clients are on the other side of the map they don't know anything about this chest when I open it up and they don't get the update but as soon as they go near the chest once they walk to the other side of the map boom you can see the chest opens up in their game is updated obviously they wouldn't see that happen because this circle is massive in a real game the only reason we have the circle so small is yet so you can actually see this easily and visualize it but there is one big problem still notice that when I walk into the circle it still plays the coins bouncing into the ear animation and it shouldn't because that happened ages ago right we were on the other side of the map we just got to the other side and it's played that animation we don't want that we want the chess to just be open right because it's not even open ten minutes ago so there's no point playing that animation and so this brings us to the final solution we actually use net multicast and rip notify together the net multicast will play the effect and the RIP notify will just open the lid of the chest so on one of my clients I'm gonna go ahead and open the chest you can see for anyone that wasn't on the other side of the map they saw the animation play but this guy in the bottom left corner he was outside of the circle so he was not receiving network update but notice once he moves across the map you can see the lid is now open it doesn't play the animation it just opens the lid this is exactly what you want you can see we use a chest open notify to actually open the lid of the chest and then we call a multicast function which plays that Gould effects which just spawned some particles so these are the core building blocks of replication you should know these really well if you ever want to work on an online game and unreal you want to learn an accurate location learn variable replication really well as well and then start to figure out the rip notify stuff server our pcs and knit multicast our pcs and also client our pcs all you need to know with client our pcs is that they're the exact opposite of the server once you have to call them on the server and in doing so it'll then call them on the client alright so now I'm gonna show you some of the city boss boss and the reason I'm showing you this a-plus-plus lost is because the main thing is to understand how networking works really this is just the code this is just the implementation but understanding the concepts is way more important but let me show you how to replicate a variable and see what's possible so let's make a float variable and I'm going to call this help I made an actor called ghost actor all you need to do to replicate a variable is put new property and then inside of the brackets just put replicate it and that will replicate a health value the last step is to implement a virtual function called get lifetime replicated props on that function looks like this create an implementation of it then called the super function and now to replicate a health value you just do do rip lifetime a ghost actor out and so that will replicate the health value of our ghost actor the server still needs to change the health value for clients to see it this will work exactly as you'd expect you can also add underscore condition and this will mean that you can specify replication conditions like the one that I talked about initial only back in the address so you can add conditions in there as well to check if your the server you can just do F has Authority this is the exact same as the switch has authority that we talked about back in blueprints to make a non rip you want to just do the function that you want to make rip so I'll just call it on rip health and all you need to do is replicated using and then on rip help and so now we just implement our function and I'm just going to do a log in here I'm going to say log health equals and then I'm just going to display the health that the player has so if the server ever changes this health variable it's going to call their own rate function and all the clients in the game will be able to log the health value so on begin play if I'm the server I'm just gonna set the health equal to 50 and this will call the on rip and the clients will see that the health is 50 lastly to do a server RPC you just do you function server and then make sure to add reliable as well and that's how you do a server function to do a client it's just client and to do a multicast that is just mute multicast remember that if you want to replicate a variable you need to turn replication on for the actor so I just go into the constructor and if I want to make this actor replicate I just do sit replicates true and that's all I have to do to get that working one thing I missed is if you want to have an honor it function you need to remember the market as a you function otherwise Unreal Engine will not be able to find the function if you're using the get lifetime replicated prop function you will also need to include the header file for all the networking code and all that is is net slash unreal Network H and now you will be able to replicate variables I'm gonna drag my ghost actor into the level and I'll just give him a static move and I'll just select the ghost down here you can see my output log if I go ahead and run a game with four players in it you can see that all the clients in the game have logged out health 50 so you can see that our own rep is working if you want the server and all the clients to call the enroute function all you need to do is change the health value and then also call the function on the server itself as well and now the server and all three clients will call the health function thank you for watching guys if you enjoyed this and want more I can talk about me working for hours there's so much more to talk about and even in an hour I'm sure I only covered like maybe 30 percent of everything there is to know but these are the core basics like these are the building blocks for making a networked game and the rest is just optimization and stuff like that so if you want to see more leave a comment and maybe I'll do like a part two or something but anyways thank you very much for watching and I'll see you in the next video you
Info
Channel: reubs
Views: 102,289
Rating: undefined out of 5
Keywords: Unreal, Engine, C++, Tutorial, Beginner, Game, Programming
Id: UmJzuRO4Qjs
Channel Id: undefined
Length: 27min 24sec (1644 seconds)
Published: Sun May 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.