Basics of Replication | Instances, RepNotify, RPC's | Multiplayer Tutorial Series | UE5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys and welcome to my video about replication basics okay so for this video um first of all what's good to know is that you can get this entire project on our patreon so link is down in the description and yeah for this video i made this little custom map here in which i will be going over some replication basics and just overall the concept of replication and what that term even means so i've made a couple of scenarios here and a couple of cool little basically actors to showcase replication so that you guys have a good understanding of how this works so let's start one player and i will start with the first thing which is that term replication so what actually is replication well uh for the non-english speakers let's first look at what the actual word replication means so to replicate something basically means to copy or to have a duplicate of something so imagine a nice painting right like this painting over here it has a nice view and everything so imagine a nice painting and then you want another copy of it then in that case you can also use the word that you want to replicate this painting so if you have a davinci painting and you want that painting a second time then you can replicate that painting in this case like it says here on the bottom you now have two of the exact same ones so basically if this is our original painting then over here we have a replicated painting now let's look at how that applies in our engine okay yeah so i made these pills and these are basically replicated so they have movement and expressions but uh these two guys keep ex uh they keep escaping their books basically but um yeah so let's look at replication so i got this example over here of how we actually apply replication now that we understand what that term is so let me read it like this now that we understand the term replication let's take a look at how this applies to unreal engine multiplayer situations in unrendered multiplayer every connected computer slash player has its own instance of a game so what i mean by that is basically when you start the dot exo file of your game then imagine that this over here represents your computer so you have your own personal computer your pc and over there you you click on the doll exe file and then your game is running now what you can also call that instead of saying your game is running is that you can call it an instance so that's what i wrote down here on the bottom then instance and every game that runes has its own instance and that's also how single player games work basically but in multiplayer that's the same stuff so you will now have an instance running on player 1. you have an instance running on player 2's computer and then you also have an instance running on players 3 computer now in order to apply replication in this case you want to have the instance of the server which can be player 1 or a dedicated server for instance you want this instance just like those two paintings over there to be replicated to all of the connected clients so over here i built this little switch and as it says here on the ground right now replication is off and you can also see these guys are moving in the tree boxes in all different types of directions so they're not in sync right now so basically to illustrate how replication that works if i am to turn this box on then you see that after a certain amount of time probably takes like six seconds for them to move into their locations then right now we see that all of these little tic tac guys over here are actually in sync so whatever is going on here on instance one pc one which can be the host of the session the host of the match is now also happening here on pc2 and also here on pc3 and that's where that term replication really comes into play so here yet again you see that they are now in sync and it also says it here on the floor okay so now that we understand what the replication is and how that works let's look at how this actually applies in on our engine so basically like we wrote over here the server here we have the server of the game server is running its own game instance of the game the server can either be dedicated or a player hosting a server so what you typically see is that bigger games there is just one computer somewhere in a data center or something like that from amazon that is basically dedicated to serving your data that's also why it's called a server and those you call a dedicated server you can rent those online and you can run your game file on them and then what you also have is that instead of just having like a dedicated server you can have actual players hosting your game and that's what you call a listen server so basically there is one host and then you have clients joining that host and then the clients are listening and that's where the term listen server comes from the clients are listening to the server which serves them all of the data okay so then what do the clients mean in this picture well basically now that we know that the server basically sends his state uh so to keep everybody in sync to all the connected clients then what do the clients do because obviously if you walk around with your little tic tac if you are that tic tac over there and walk around then you want that to be sent to the server so that the server can then send it back to all the clients and keep everybody in sync so that's what the clients do clients receive updates from the server as it says here about the server state this way the clients can update their own instances to be in sync with the server state server instance it is also the client's job to send their data to the server so that the server can broadcast that information back to all connected clients and thus keep everybody in sync so that's the task of the clients and essentially the server is a client as well if the server is actually a player hosting a session and not a dedicated server as you as a server are also having input and sending that to all the clients and the client is also having input sending that to the server and having the server send it to other clients and then we can turn off the replication here and each instance each computer here is now doing their own logic again and you will see that they are now out of sync alright i hope that as a good example and a good understanding of how this works so then let's look at what i wrote over here so basically over here i said how does unreal engine handle replication because now that we understand the concept how do we actually apply all of that stuff well i wrote that here in three little steps basically the first thing that we have to do in our engine is that once you go into a class you have to turn on a variable called replicates and once you turn on that variable called replicates at that point if we go back over here to this example at that point the server will then basically have control over these other clients now that does not instantly mean that everything that happens on the server automatically happens here on the clients no if you turn that replicates variable on the server now basically has the ability to override the clients and that you can also call correct the clients but as we saw before it is also the client's task to send their information to the server because otherwise the server can simply not know what the clients are doing and if the server does not know what the clients are doing and doesn't get any signals from the clients then it can also not broadcast their data and make sure that everybody is in sync so if we turn on that set replicates to true then now we basically enabled the possibility for multiplayer but then we still have to program multiplayer okay then let's get back to our example over here okay so that's how we turn on replication well then let's look at the second thing so like i just explained now we actually have to use stuff to make the multiplayer happen because now we just enabled it and we i will show you where you can find this replicates in a second but now we actually have to make sure that we make the game replicate and that the clients can also share their data with the server and vice versa so for that we have variables in this is just general in programming you have variables and variables contain values so in unreal engine there's two types of variables well actually three there is a non-replicated variable that just means that it just exists on your instance of your game and it's not replicated so it's just happening on your computer and your computer only then we have a version called replicated variables it's the one that is that you see here in blue basically what that means is that that variable now exists on your server instance and on your client instance but the server now has control over that client-side variable so what that means is that most of you have probably heard of a term called ping or latency so based on your ping the server sends the clients all types of updates and behind the scenes this is all handled by onward engine already for us so we don't have to worry about that but once a variable is set to replicated then the server-side version of that variable will basically correct the client-side version depending on what your latency as a client is or your ping so now that it is set to replicate then the server will try to do it best to send as much updates to you the client as possible and then there is another version of a replicated variable and that is what our engine calls a wrap notify variable and as the name suggests it notifies you of something with a replication so basically when replication occurs it will notify you and in reality that wrap notify how it works is that imagine that you have a variable and let me actually go ahead and show you this right now so let's open up this blueprint over here okay so let's create a new variable so that i can showcase what this wrap notify does so i will call it just variable so imagine that you have a variable right this one over here currently this is not set to replicate so my game it will do this on this person's game it will do this and on the third person's game it will do whatever it will it can have different values for all these clients now if i set this guy to replicate over here so in the details panel when you select the variable set it to replicate now we see these two little balls on top up here and what that means is that basically those balls indicate the server state and the client state being in synced that's why they are also overlapped so if we take a look here at our character i remade this basically so here you have the server state and the client state overlapping each other which basically means that they are syncing with each other and the the reason why they why distilled it so why one ball is on top and the other on the bottom is because the server sends the information down to the client and that's how replicated variables work so once you turn a variable here to replicate it you will basically get those two balls here and now we have our server side variable so here we have the variable on the server and then over here we have that same variable on the client and that server one will keep correcting that client one every little update that occurs in multiplayer and updates happen very quickly there's super low latency nowadays but if you're playing with somebody from japan and you're situated in germany for instance then there might be a little bit more latency going on here all right so that's what how you do replicated variable but then let's talk about that last one that wrapped notify variable so once you turn this on you see that in the variable nothing changed but here on the left we just got a new function and that function is automatically created for us once we switched this replicator to wrap notify and this function here you cannot double click it and open it up can contain some logic and you can it's up to you to program whatever kind of logic you want in there so uh regarding the variable well nothing changed as you see it's still replicating but how does this work well every time that the variable gets changed so let's say over here that the variable got changed from to true to false so every time that your variable is out of sync and basically gets changed or updated then automatically this wrap notify function here gets called so what is this useful for well basically let's say that this variable over here is set to false and now it's turned on to true if you want to display that in your user interface you don't want to like event tick you don't want to untick check basically whether or not the value has already changed no you want that automatically to happen and that's what our engine made that wrap notify for so every time that the value inside of this variable gets changed your unwrap gets called okay now that we understand what an underwrap is then we can walk to the last part here of the puzzle over here the last part to make a multiplayer work in our engine is remote procedure calls and they are also known as rpcs so what are remote procedure calls well in our engine there's three types of remote procedure calls run on owning client run on the server and multicast so let's take a look at what those are so back into my blueprint if we just open up this guy here because you can have remote procedure calls in any blueprint but back in this blueprint let's create a custom event so we do a custom event and call it do something just like this okay then by default when this event gets called so if i type do something and i have a button called this event here then by default this event just runs on that instance of the client so like we saw here it will just run on the instance of the client because it is not replicated then over here you see some remote procedure called options so what we can do over here is basically set it to run on the owning client and you see executes on owning client so now this event only happens on an owning client and then over here we see a target so here you can then specify on who you want to run this event well right now since we are in this blueprint itself the target will say self but let's say that you call this event from another blueprint then you can input a target here on who you want to execute this function then we also have here run on server so this means that this event will now be run on the server and on the server only so to put that into perspective again here we have the server if we use an event called run on server and we call it from a client so the client presses a button renault servant fires then the client basically executes event that happens here on the server and then the last one that we have is here the multicast and what does that do well the multicast can only be called when on the server so if we take a look over here then basically the client let's sync them again then basically the client can send a multicast event to the server but then nothing will happen because like i just said multicast can only be executed from the server so in this scenario the server can call a multicast and what a multicast does is that it broadcasts an event on the server itself and on all of the connected clients so let's say a particle for instance that you want to replicate it then the server would basically call a multicast event where it would create that particle and then the particle would appear on the server and like i just explained since it broadcasts it to all the clients the particle will also appear on client one and client two etc etc so with those events you can very specifically target which events gets executed where and for which players it's visible so back to that owning client one that one is intended to be used on the server so let's say that the server wants to send a certain event to one specific client let's say that it wants to update client number one's ui but specifically client number one now what the server can do is that it can find client number one in its server array of all the connected clients and run an owning client event on specifically that one client so that's uh for you to understand what rpcs are useful for and how you can actually apply those now in my future videos from this series i will go into each one of these specifically so i will go specifically into owning client events and examples for them specifically into server events and examples for them and specifically into multicast events and create great examples for them so that you guys have a good understanding of how this works okay now that we understand all of these basics then let's look at an example that i have over here so this is a replicated actor example well as we see it has that those two balls above its head and those are basically uh the symbol of replication in our engine so right now one is white the other one is red that means that this one is currently this actor here is currently not set to replicate so it will only show on the server so let's actually create two players over here so if i go into our engine here to the top select number of players 2 net mode listen server which means one player is the server the host and the other one is the client listening to the host so if we do this and start then let's put a window on the left and a window on the right now first of all on top here we're gonna see client one so this is the client and here on the right we are seeing server okay so now we have this guy over here and this guy is basically this instance here and now we have this guy over here which is this white pc over here well then let's take a look at the guy that i have here as an example of what our application is so let's walk to him okay what do we see here well since the server state is not replicating to this client guy we see that this guy which all he does all the logic that he has is that he just rotates randomly so we see that on the server he's now looking to the right over there and on the client we see that he's now just looking straight forward and that's basically all he's doing and whatever that server is doing that client guy is really not up to date of what is happening here so let's look at that character and then like what we saw over here the turn on replicates let's try that and see what happens so inside of this blueprint just to quickly talk about the logic here all it does is that it just starts to move basically on begin play and then it will start random movement here which is has replicated variables so this data inside of here is set to replicate so basically the actor here starts to move but only on switch has authority which means authority is the server remote is the client also this type of stuff i will go over in more detail in upcoming videos but basically on beginplay only the server will execute this logic now what this means here is that this event here even though it's not replicated or set to run on the server this currently just executes only on the server because we're running it off of the authority so on the server we are getting this actor location so this guy and then basically we are getting a random reachable point in a radius of 300 which means three meters and that's the location that we want to look at basically so we just store this in a server variable called random location since that is set to replicate the clients will also know what values are in here and will also automatically be updated with these values even though they never ran this event here and even though they never run this logic and then we just set whether or not this guy is rotating to true which is also replicated and then the following logic is on event tick so this happens for both the server and the client because it's not specified with the switch has authority whether or not it only happens on one of them so basically this happens on server and client when rotating is set to true and we get that by replication then the actor starts to rotate into a certain direction and then after rotating basically we wait a couple of seconds and we rotate again so that that's literally all this code does so as we saw right now it was not in sync if i then go to my class defaults i get this replicates variable here and set it to true then what happens is that then if we play it with two players like this get one guy here to the right and the other one to the left then we see that the two balls on top of the guy's head symbolizing replication are now both white and we see that both on the server and the client we are seeing the exact same movement of the sky so yeah i hope that this has gave us a clear example of the basics of replication and also just the term of replication and an introduction to that first of all you must turn it on in order to be able to work with anything regarding replication and then besides that you basically have to use replicated variables or wrap notify variables and then last you have to use remote procedure calls in order to make the clients also communicate with the server okay guys if you're interested in this project and taking a look behind the scenes of how i realized this then it's available on our patreon and in the next video i will be going in more detail with actual examples of replication so i will talk about for instance those replicated variables and wrap note device and show some proper use cases and i'll show use cases of the owning client and other multiplayer stuff here as well so owning client rental server and multicast okay guys see you in the next video bye [Music] oh [Music] you
Info
Channel: Kekdot
Views: 25,931
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: OVeo3cVTIcU
Channel Id: undefined
Length: 22min 40sec (1360 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.