🎮 UE4 Replication - Part 2: Game Instances

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it has been 121 days since the first video came out and I just want to say that I am sincerely blown away by the fact that you guys still think I'm gonna finish this series I'm just kidding of course I'm gonna finish this series you guys have been extremely supportive so I do want to apologize it took so long and let you know your patience is much appreciated so without further ado let's dive right into a long-awaited part two of our replication series in this video we're gonna cover game instances we're just gonna scratch the surface here but if you manage to understand this piece you're well on your way to creating the next fortnight I'm kidding we're also gonna learn about server authority and when and why the server has authority over certain actors components or variables we're gonna really understand how this works by learning how to properly replicates printing and how to improperly replicates printing as well and then we're gonna have a little fun by creating our own anti speed hack cheat detection system this is a hidden secret that you won't find anywhere in the ue4 documentation so stay tuned until the end and you won't regret it alright so launch and real let's create a third person project with no additional content just to keep our project size small and we're gonna name it playground since we'll be using it for future lessons and examples once the project is created we'll adjust our screen settings so we can fit two windows nicely side-by-side and to do that click the arrow beside the play button and go to advanced settings under window width and height we can set our width to half of our resolution my resolution is 1920 by 1080 so I'm gonna change the width to 1/2 of 1920 which is 960 and I'm gonna use the full height so 1080 for the height now if we change our number of players to 2 and then select play in new editor window this will give us two windows that fit nicely side-by-side just like that beauty at this point we've already created our very first listen' server if you look at the top of the left window you'll see that it says server and we know it's a listen server because there's also a player playing on it as well and on the right side you'll see that it says client this client is connected to the server or this other player over here who happens to be the host by setting our project up this way we're going to be able to see what the server is seeing when a client does something and what the client sees when the client does something so let's open up our third person character blueprint you can find that in content third person BP blueprints third person character so let's make our character run faster when we press a key right-click on the blueprint graph and type in left shift this will give us an input event listener and it basically just listens for the user to press and release the shift key using exact key names like this is not the way you want to configure your controls what you want to do is use input actions and input axes and that'll be something that I'll be covering in the ue4 basics series when we recreate the third person template whenever I get around to getting that done like get out of here anyway so a good practice is to make sure that your event listener is listening and responding properly we can do that with a simple print string so we'll create two of them and we'll connect one to the pressed and we'll connect the other one to the released when pressed we're just gonna say sprint and when released we're just gonna say walk let's hit play and test it and now you should see the print string as you press and release shift notice that when doing it on the Left screen it prints on the server and when we do it on the right screen it prints on the client this is telling us where our code is being run even though this is the exact same third person character blueprint it behaves differently depending on where the code is being called we'll dive deeper into what's actually happening here later in the video but for now let's make our character sprint so if we look at our third person character blueprint we can see that it has a main actor as well as some components attached to that actor one of those components happens to be character movement you can see that the character movement component has a section called walking and it has a property called max walk speed if we drag the character movement component onto our graph and drag off of the pin we can type in max walk speed and see what we can get looks like we can either get the value or set it let's set it to 1,200 on pressed and then if you just click the node and hit control W it will duplicate it for you connect the character movement to the target and on released we're gonna set it to the default which was 600 let's tests you'll notice that when we try to sprint on the server everything works perfectly fine however when we try to do it on the client nothing is happening okay well why you can see that the print string is happening on the client side it works on the server because the server has authority to change the speed whereas the client does not so let's ask that question again why why does the server have authority over changing the speed while the client doesn't great questions guys the server's job is to serve data or replicate data to all connected clients the word replication itself means to make a copy of something or reproduce in our case when something is set to be replicating it means that the server is passing some data to a client who's gonna be reproducing exactly what's happening on the server but in their own instance of the game so back to our original question for the third time I promise I'm gonna answer this time why does this ever have authority over change in dispute what the client doesn't well the answer comes in three parts uno the client can indeed change his own sprint speed dos the server can change the client speed only if the sprint speed is replicated tree if the actor movement is replicated in the server speed does it match with the client speed then the server wins okay so why does the server win well in order to keep the game fair we need one true version or copy of the game these copies of the game are for to ask game instances since clients can do whatever they wanted their copy of the game we don't want to perform any important gameplay functions that could give them an unfair advantage over other players instead we do it on the server that way if the client says my sprint speed is 20,000 and I have unlimited health if these values haven't been set on the server the server is gonna send an update to all connected clients and it's gonna send the true copy of what those values really are okay let me illustrate just to drive this game instance thing home I really want you guys to get this so here's our server and our server has its own version of the game running this is the game instance and this is the one true copy of the game that we care about so when a client launches their game they create their own instance of the game the client then connects to the server and based on the level or map that the server currently has loaded there is a game mode associated with that level or map the game mode determines the game rules for that map as well as some default classes to use such as what pong or player controller the client should be assigned when they connect to this map we can see that on our third person game mode that the default pawn class is a third person character blueprint based on these rules of serve response a third person character for the connected client and the third person character blueprint may have some variable set such as this print speed in the walk speed then the client spawns their own version of the third person character in his or her own game instance which has its own set of sprint speed and walk speed variables remember these game instances are not the same world think of them as parallel universes so the server world has its own copy of everything and the client world has its own copy of everything what another client launches their game the same thing happens a new game instance is created for that new client when they connect to the server based on the level in game mode the server spawns a third person character for that client and sets whatever variables or components exist within the third person character blueprint then the client spawns their own copy of the third person character in their own game instance if we look at the third person character blueprint we could see that it's set to replicates and the relevant two owner only checkbox is not checked so the server replicates it to all connected clients so the server's gonna tell greeny over here that blue he just joined the server world and that green II in order to reproduce what the server world has needs to make a copy of blue e in his or her own little world likewise the server tells bluey that he or she needs to make a copy of green e in his or her own little world similarly just so we can have all the colors yellowish-orange e launches their game a new world is created for them they connected the server service pawns a third person character than yellowish-orange e spawns their own version of the third person character in their own little world and since the third person character blueprint is sets replicate to all connected clients the server tells yellowish-orange e about blue and green e and then it also tells green and blue a about yellowish orange e did you guys get all that if not let me know in the comments and I'll try to explain it better I don't know how I'm gonna do a better job than that awesome illustration that you guys just saw but I'll give it my best shot on a serious note though let me know if you guys actually like these visual illustrations cuz they're pretty time-consuming and if you guys don't find them helpful at all then I could save myself a lot of effort but if one person says it helps them then I guess for the sake of that one person I'm not gonna do for one person forget that guy I'm kidding of course I'd do it for that one person just let me know in the comments if you are that one person alright let's get back to work so jump back to the playground and hit play when we try sprinting on the client you'll notice that there's a stutter this is an example of the movement being replicated and the client and the server are fighting over the sprint speed and the server is winning we can see this much better if we go back to our third person character blueprint we look at our character movement component and we set the starting max walk speed value to something like 20 and if we test again remember point tree from earlier if the character movement is replicated and the server sprint speed does not match the client sprint speed the server wins so in our case we've set the max walk speed by default to 20 so a third person character blueprint is being spawned on the server for this client but then this client is pressing shift to sprint and we're setting the speed to 1200 but we're only doing it on the client side here we haven't made any changes on the server yet if you press your tilde key to bring up the console and if you can't see it it's probably hidden at the bottom so just press it again and they'll come down from the top type in the following command p.net show Corrections spaced 1 this command helps us visualize corrections that the server makes to the position of the character so if we try to sprint on the client once again you'll notice that Christmas has arrived early if you are red-green color blind I apologize I tried to find a way to change the capsule colors but I haven't found it yet what you're actually seeing or not seeing I guess in that case is that there's two capsules one is red and one is green the red one is in front of the character and the green one is behind him so I've captured a set of these capsules and what this basically tells us is the red capsule is the clients game instance calculating where the clients should be based on them pressing their shift key and setting their speed to 1200 the green capsule is the server sending its version of where the client actually is based on the speed that the server has which is 20 because we never changed it that was the default we set the max walk speed to since movement is being replicated and we know that replicated variables give the server authority the clients position is corrected and he's moved back to where the green capsule is ok so instead of running the code just on the client let's send a request to the server telling it that we want to sprint right-click on your blueprint graph and type any custom event custom events give us the ability to run code on the server again this is what's known as an RPC or a remote procedure call we'll name 1sr underscore sprint and then we'll create another we can quickly duplicate by hitting ctrl W on this event and it'll give us another one we'll name this other one SR underscore walk you'll see some folks use ser as a prefix but I find that ser brings up other options whereas SR just brings up your events and it happens to be one less key to type and I'm a lazy guy so logically it makes more sense to use SR just saying this is just my naming convention that I like to use but I encourage you guys to do the same or come up with your own but most importantly keep it consistent because consistency and a little organization early will prevent you from hulk smashing your project and starting a new one alright select the custom events that we just created and we're going to change the replications property to run on server you can find that under the details panel here and what we're gonna do is we're gonna connect our set max walk speed nodes to our new our pcs now we need to call these our pcs when the shift key is pressed and released right-click type in SR and oh my goodness look how easy that looks up will connect the pressed pin to the sr underscore sprint and will connect the released pin to the sr underscore walk hit play interesting my client is running faster but there's still a stutter if we take a look at our Christmas decorations again we could see that the capsules have now swapped positions the client is pressing his shift key which sends a message to the server saying hey I pressed my shift key server says ok this means your speed is now 1200 so now every time the server sends an update to the client he's getting teleported forward because we're not changing the speed on the client side by default our max walk speed is still at 24 a character movement component so the client is running at 20 speed and when he gets the update from the server he gets teleported forward keep in mind that other connected clients are not gonna see this struggle with this client that doesn't know if he needs to run or walk the server is just sending them information and they're just replicating that information in their own little world we can quickly see this in action if we change the number of players to 3 and we'll put client 1 on the very left side and client 2 will dragged over here to the right so now if I sprint on client 1 you could still see that this client is struggling but on client two's view he's running perfectly fine because the server is telling client to that client 1 is running at 1200 speed client 2 does not know that client 1 thinks she's running at 20 hopefully that makes sense if not hit me up and I'll try to explain it better but for now we're almost there so let's fix the stutter copy and paste a set max speed nodes and connect them to the client either before the RPC call or after it doesn't really matter I'm just going to connect them here before and then if we hit play voila it works great we're setting the walk speed on the client and then we're setting it on the server as well so they match and no more fighting notice how there's still a correction happening when I toggle sprinting though this is inevitable because there's always gonna be some sort of delay between the request to the server and the response back the client is pressing their key he's sending the request and changing his speed right away on his game instance but by the time the server sends a response back it's a couple of milliseconds later based on when the server set his speed to run faster it sends him an update of where his position should be and that's that minor correction if you've been following along you'll notice that when you try to sprint these minor Corrections are not really that noticeable with that being said let's take a look at what not to do when calling an RPC from a client to a server let's drag off of the set max walk speed input pin and drop it on the server call this will create an input where we can pass data from the client to the server and we'll do the same thing for the walk server call let's rename the SR sprint input to sprint speed from client and we'll rename the SR walk input to walk speed from client so what's happening here is when our client presses shift we're setting the max walk speed to 1,200 and then we're passing that same value to the SR underscore sprint RPC and we're doing the same thing when the client releases shift but with 600 speed instead and then that gets passed to the SR walk RPC on the server side we're just taking those inputs and we're setting the max walk speed on the server so there's no fighting and everything should work fine just as before hit play and test and there you go let's just pretend for a minute though as hard as it is to believe that our player is not trustworthy and somehow they've managed to change their sprint speed from 1,200 to 12 million guess what's gonna happen it's gonna go right from the client to the server and now you're getting sued by Marvell for copyright infringement because your player is now the flash this is why I always tell you to never trust you know what comes next your players it's not because I have trust issues okay maybe it is but at least now you can understand why let's have some fun with this looks a lot of these values to be passed in and we're gonna create two flow variables one will be called sprint speed and the other one will be called walk speed quick compile so we're able to set some default values we're gonna set the sprint speed to 1200 and the walk speed will set to 600 I'm gonna clean up by disconnecting the float here and grabbing another reference to the character movement component plug it into the target and move everything over to the right just to give ourselves some extra space we'll add a branch on our server calls so hold B and click on your graph then drag off of the flow pin coming from the client and let's compare it to our default variables by using our equal operator connect the boolean output to the branch true we set the speed let's make this even more calm by changing the sprint speed and walk speed variables on the client when they press the shift key so we'll set sprint to 12 million and we'll set wok to 600,000 and then we're going to pass that value to the character movement components set max walk speed which is then going to pass it to the server calls as soon as this RPC is called were on the server version and the server's game instance has its own version of the sprint speed and walk speed variable and it's still set to 1200 and 600 by default oh gosh not another one no I know I know it's terrible but let's visualize what's happening here we're already familiar with these two game instances that have their own copy of each blueprint one on the server and one on the client client presses his shift key and sets the variables to the new values then those variables are passed to our server calls this sends a request to our server with the clients version of that variable we compare it to the server's version of that variable since the server's version of that variable doesn't match the clients version of that variable the server should then send a proper response to the client such as I don't know a good old introduction to the booth back to our graph right click and type in append and this node simply builds a custom string but there's a hidden secret that you won't find anywhere in the ue4 documentation even epic doesn't want to let you know about the secret but I'm going to reveal it because I like you guys let's get a reference to self and we're gonna plug that into the a of the append node and in the B we're gonna type in space deleted by JC - exclamation marks no more no less we'll connect the output pin of the append node to a print string and then we'll connect the branch a false execution pin to the print string and we'll drag off of self one more time and type in destroy actor and we'll connect the execution pin from the print string to the destroy actor hit play and now when we try to sprint us yeah I couldn't find it anywhere so there you go so let's quickly recap in this video you learned about game instances and the importance of having one true version of the game when and why the server has authority over certain actors components or variables how to create and execute our pcs how to properly and improperly replicates printing and finally how to take your incorrect replication of sprinting and johncena proof that thanks again for watching guys if you learned anything today I need you to smash that like button and johncena that sub button and on a scale between you hate puppies and ten let me know in the comments how many times you laughed while watching this video I'll see you guys in the next one but until then I'm not gonna lie it may be a very long time peace [Music]
Info
Channel: BRY
Views: 9,756
Rating: undefined out of 5
Keywords: unreal engine 4, unreal engine, unreal engine 4 tutorial, unreal engine 4 games, unreal engine 4 blueprint tutorial, unreal engine 4 tutorial beginner, game development for beginners, game development tutorial, unreal engine tutorial, ue4, ue4 tutorial, ue4 multiplayer replication, replication, multiplayer replication, how to replicate, how to create a multiplayer game, how to make a game, ue4 blueprint tutorial, multiplayer, networking, repnotify, game instance, game mode, unreal
Id: whow14uFWtw
Channel Id: undefined
Length: 19min 52sec (1192 seconds)
Published: Tue Nov 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.