[UE4] The Secret of Multiplayer | Ownership

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right guys welcome back to another video in this one we're going to be going over the secret of multiplayer i'm going to talk shortly about a very important networking concept that a lot of basic networking videos don't seem to dig into too much before i get into this i just wanted to say i'm going to get unnecessarily technical so if you don't want to hear me ramble on for five minutes straight about ownership i'll just give you the summary right now the secret is ownership if you want to replicate a custom event inside a blueprint you should only let that event be called by a client who owns the actor in the following example i demonstrate and recommend intermediate developers to call all of their server side events inside their player characters or player controllers as both of these actors are owned by the client so they have the authority to send packets to the server telling it to execute the event on the actor on behalf of said server keep in mind the actor itself does not locally execute the event it just receives the relevant information from the server which it ran on anyway on with a video before i start i'm assuming you have a basic understanding of the engine and i'm also assuming you understand client server communication the server talks to clients and not vice versa anyway i'm going to show you a really basic multiplayer system in a game that i'm working on called civil essentially you can see i have a line trace going around my head right now as i move around it's kind of weird but when i'm looking at a plant or a farm plot i can actually harvest and plant seeds and the way this works and i'll actually show you this is a multiplayer system that the client too can see this these are both clients running on a dedicated server so for example if you look on the top screen i can plant plants on this screen as well and here i'm just going to kind of talk about the communication system between blueprints and how all that networking works and this is where the big seeker comes in i'm going to read you a quote from a blog post in a second here but not before i show you the blueprints you're seeing here so you have a player character which is right here you have the farm plot and you then you have the farm plot parent so this farm plot basically comes from a parent class which is just called interactable and then this is the interactable act this is the actual system that the player can look at and interact with which is the farm plot itself where we run all the farming logic and then of course in the player where we actually run on the netcode so why am i running the netcode inside the player and not the farm plot this is a fantastic question and that's why i'm going to get into the blog post the blog post says server rpcs can only be called by a client who owns the actor executing the event for example a player pawn is owned by the client who controls it only that client can send server rpcs on that pawn so essentially what that means is if i went in here and tried to run a custom event on the server what would happen is if i go into the player and grab the interactable we're looking at and then just try to run that custom event well it's going to work if you are the server but if you're a client it's not going to replicate that down to other clients in fact it's probably not even going to run on your screen it's only running on the server so how do we go around this well the way the way this system works is essentially i have my looking at system which is just a line trace you saw the line trace when it when it hits that interactable it just asks if it is the interactable gets the reference to it and then sets it as its own separate variable then it calls to look at something and then when you press the interact button on your keyboard it runs this gate and then it decides to interact on the server when you're actually looking at it and of course it only interacts with one interactable at a time because we're storing this as a variable this isn't even a replicated variable or anything because now we're ready to run this on the server this is going to work for pretty much any interactable because they're all going to come from the same base class i recommend using parent and child classes so that you can just use the parent class for everything you do in that interactable and then that interactable itself the parent class will have dispatchers which you can call for your networking so i don't even need to use custom server events on this none of my interactables will ever have server events that run on them now back to my player here's where the server logic happens if i'm going to be multicasting anything or running anything on the server i'm going to run it from this class because this class is owned by the client and can be seen by the server so here i can finally run my event i have my interactable as a reference and i'm not going to be grabbing it from anywhere else because again it's not a replicated variable it needs to be put into here and then used as an actual output and then from here it can call its own dispatcher where the dispatcher will run this event which is technically ran on the server but doesn't appear to be because we know that the dispatcher is going to be called on the server and then it can run all of the farming logic and some client logic that i'm also running using this dispatcher which is completely unreplicated and that leads me to the next thing that i was going to talk about really quickly which is essentially how am i handling replication well the farm plot itself is replicated and the plant itself is a replicated component those are the only two things that i need to be replicating here and i hope that this makes sense i know i know that i'm going way in over my head here with this but i have two simple blueprints one of them has things that i want to exercise on the server the other thing actually has to authorize those things so the player is authorizing this runs it on the server calls a dispatcher which could be called anywhere which could be called locally which could be called server side or multicast on the separate blueprint that i actually want to affect i hope that made sense i know i'm probably going really fast here and if you have any questions feel free to go and join my discord or put them in the comments section below i'll try to get to them thank you guys for tuning in don't forget to like and subscribe and i'll see you in the next one oh and don't forget to check out civil at playcivil.com hey i gotta promote my project somehow
Info
Channel: Aaron Hunt
Views: 19,282
Rating: undefined out of 5
Keywords: game dev, music, art, film
Id: k4_n5LD5Ees
Channel Id: undefined
Length: 5min 23sec (323 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.