Getting grips of the UE4 Network framework
Hi, guys I'm currently struggling to understand how the UE4 network framework works. I'm sure I'm just being slow so I was wondering if anyone could explain in detail how it works. Specifically things like replication, events that use multicast, run on server etc. I've read and watched quite a few tutorials on how it all works but I still can't get my head around it. No worries if it's too much trouble, but I think it's easier to learn when someone explains it to you rather than just does it in a video for you to copy.
asked Nov 12 '16 at 11:28 PM in Blueprint Scripting
Sure. I'll try to explain :D
Look lets imagine we have a multiplayer game where we have one server "S" and two clients, "C1" and "C2". So that is 3 instances of the game running. Now let's focus on C1. C1 has a character actor. Now this character does not only exist within C1, a replica of it also exist in S and C2 so that is 3 characters, one in every computer running the game. To make it so they all look the same or synchronize them is through replication and the way that works is something like this. S contains the "real version" of C1's character actor, in fact S contains the real version of every actor in the game. C1 and C2 only have the "fake version" of the actors. S tells them every frame about any change on his "real version" so C1 and C2 can update their "fake version".
You may ask, yeah but how do I make all of that happen?
We do it with replicated variables and Remote Procedure Calls (Events that use Run on Server, Multicast or Run on Owning Client). Ok, it should be clear that when you make a blueprint and put some code and variables in it, both server and clients are gonna use that same code and variables. If you mark a variable replicated, for example you do it with a health variable in your character, S will know that he should take the health of his real version of the character and send that to the fake characters in C1 and C2 so they can put that information in their health variables. If C1 tried to change his health variable by himself it would only last for that frame because it would be overridden by the one that S is sending him, and also no one else but C1 would know that he tried to change it (C1 trying to cheat and stuff D: setting his health to 999,999,999, not gonna happen >:D).
Like I said before when you put code in your character blueprint, every body, in this case S, C1 and C2 will have that same code, but how do account for that when you are coding? how do you know when a blueprint executing in S, or in C1, C2? You do it with the Switch has Authority node, you probably have seen it in tutorials. If it has authority then your code is being executed in S, if it is remote then you are executing code in any client, C1 or C2. So if you wanted to change a replicated variable like health, would you do it in the authority line or the remote line? Answer is authority line, because, like I mentioned, S contains the real version of your character and he will tell everybody else if something changes. If you change health in the remote line it would just be overridden.
Now let's take a look at RPCs (Remote Procedure Calls). Let's say that you have created a function in your character that makes him attack and you have your input setup in your blueprint so it executes this function when you press the spacebar. If you didn't account for networking in your code and let's say that C1 presses spacebar so he runs your attack function. You will probably end up with something like, C1 doing the attack but S won't know about it and thus C2 won't know about it either, for them it will look like C1 never did the attack. If you want C2 to know that C1 did an attack then S is the one that should tell them so S himself needs to do the attack. First thing you should know is that input is local. Input only works for C1 or C2. S does not know anything about input his only job is to send the information about his "real versions" of the actors to every client. So then how do I tell S, that C1 pressed space bar, so that C2 and any other client can see my attack? You do it through RPCs.
Inside your actors you can define events as RPCs. RPCs are basically for executing functions in the other version of your actor, for changing a variable or doing an action, anything you want.
I hope that this helps you understand a bit better. I had a lot of trouble when trying to learn this stuff, the best thing you can do is practice and research and test. Note that there are plenty of exceptions and edge cases on all of this but what I described were the most basic situations you will encounter.
I hope it helps you instead of making you even more confused xD
If you have more questions about this, fire away! I'll be around :D
P.S. Sorry for the text wall D:
answered Nov 13 '16 at 08:25 PM
Networking and Multiplayer
Unreal Engine explaining networking in blueprints playlist
Good videos on how to network the game Battery Collector that Epic made using C++
(It's not in a playlist for some reason, but there's 20 videos)
Unreal Engine Blueprint Multiplayer playlist
Follow this question
Once you sign in you will be able to subscribe for any updates here