Networking in Unreal

I’ve spent a fair amount of time working with Unreal 4’s networking package, and would like to compare it to what I’m used to, namely Photon Unity Networking. I want to compare the two in how they handle some of the big networking concepts.

Setup

Bam. Done.
But really, like UNET and unlike Photon, Unreal 4 networking doesn’t support NAT punch-through, meaning that we’re restricted to LAN games unless we implement a third-party library. This is where something like the Steam SDK comes in handy. That said, Unreal is prepared for this kind of stuff, using the IOnlineSubsystem interfaces, which will tie together multiple online gaming services: I’ll be looking into this and going over it further in the future.

RPC Functions

Just like in Unity, RPC functions need a header. However, these function headers need to define who the function is going to be sent to, how important the packet is, and if it should take anti-cheat measures.

In this example, requesting health is going to be a function that users ask the server to do for them, the packet needs to be reliably sent, and a verification function needs to be implemented.

In the .cpp file, the exact function with the same name is never actually written: Unreal handles that. Instead, a function with “_implementation” appended is where the code for that function must happen.
Additionally, a “_validate” function must be written. It returns a bool, which if false, will kick the user for trying to cheat. This is where the server must determine what would be out of place: what would be an impossible amount of health to pick up? What would be an impossible speed to reach? Etc…

Synchronization & Smoothing


As far as smoothing goes, objects have a “Replicate Movement” option, which updates positional data over the network and smooths (interpolates) in between packets received. The result was very accurate and responsive.


In C++, doing this is as simple as setting the character’s “Replicates” and “ReplicateMovement” values to true in the constructor. Additionally, meshes and components can be set to replicate.

These values can also be set in the character’s blueprint, under the “Replication” category.

Synchronizing specific data, such as a character’s member data, requires adding the GetLifetimeReplicatedProps function to the .cpp file, and feeding the data into a macro, like so:

Callback Functions


Additionally, in a piece of data’s UPROPERTY header, callback functions can be defined. These functions will be handled when the variable is received over the network.

In this case, mTask is an enum that handles shooting, reloading, etc… and when a client receives someone else’s mTask value, they’re going to figure out what to do with it.

Checking Authority

Lastly, one thing that’s pretty vital to networking is understanding who owns things, and who’s the host. Unreal makes it pretty simple, with GetNetMode(), which returns the authority of the local player.

You can check if you’re the client, the server player, or the dedicated server pretty easily with this trick.

My Take

Overall, I was incredibly surprised at how much abstraction comes with Unreal 4’s Networking package. I was expecting networking to be much lower level than what I normally do in Unity C#. I’m used to having to come up with tricks to accomplish things like:

  • sending GameObjects over the network to get around limitations on data types to send through streams.
  • having to handle smoothing myself
  • handling lobby setup.

I’m eager to try to get under the hood with Unreal’s system, however, and try doing things my way.

One Comment Add yours

Leave a comment