Download

Networked Games - trying to understand the method behind the madness

In a networked game, I’m trying to understand what I need to replicate, i.e. what to flag as “Replicated”.

Let us say I have:


class ANanoFactory : public AActor

These factories are placed around the map producing stuff as per AI instructions and can be destroyed by players.

If I set


bReplicates = true

in the constructor it will replicate the actor to remote machines. Does that mean the server will take charge of the actor and replicate to clients, and if


bReplicates = false

then what happens? Does it mean that factories placed in the Unreal editor only exist on the server and are not rendered on clients and if a player triggers an event that spawns a factory actor then that factory only exists for that player? Despite reading lots of articles, posts and wikis on replication I still struggle to wrap my head around this.

Presumably I need only set the “Replicated” UPROPERTY for variables that need to be handled on a client. So for


int32 Health;

it might make sense to set this to UPROPERTY(Replicated) if there is a visible damage bar, but if not then can I just not replicate it and the server will handle Health? Presumably if Health reaches zero then the server can issue a notification to clients to apply the explosion effect using a NetMulticast RPC (Remote Procedure Call), and if the server updates the mesh from a working factory to a destroyed factory that will be updated on all clients?

How UE4 does networked games is really cool, but clearly I need some pointers on how to use it correctly. Looking at the Shooter example just confuses me further as for example RunningSpeedModifier and BaseTurnRate are not replicated, but CurrentWeapon and bIsTargeting is replicated and I don’t get (yet) how this is determined.

If bReplicates is false on your nano factory there will be independent versions of the actor on all clients and the server. They aren’t synced in any way.

Health you will probably want to be replicated. If you want something to happen when health is changed on the server and it gets replicated to clients the cleanest way is to use a repnotify function. The function is only executed on clients when the value is changed after being sent from the server.

Also in blueprints I know there is a property to specify whether the object should be spawned on clients or not, which is checked by default even if “Replicated” is not. In that case what dzeligman described will happen. And you would spawn something only on the server if that is unchecked. Haven’t checked what it is in C++ yet.

Thanks, I’ve been doing more research and testing things and I’m starting to get a better grip on it. I think the two major parts of getting multiplayer code built correctly from the ground up is to correctly handle items and objects (AActors) so they are correctly replicated based on if they are visible to everyone (e.g. out in the world) or only to the server and a single client (e.g. inventory), and also handle movement and actions. My biggest headache at the moment is learning what should be handled on the server and what the clients can do without waiting for its authority (I’m very naive on multiplayer coding, but so far it seems that almost everything needs to be done server side which seems like a big bottleneck to me). I’m sure that is not the correct way to divide this, but that is how I’m tackling it at the moment. I also watched the Unreal youtube series on Blueprint networking and it is a great primer for understanding the essential basics of Unreal network replication.

I’m also making things more complicated by using APawn instead of ACharacter (the latter handles the movement replication), since the game is about piloting vehicles. I got it partly working by using RPCcalls to handle all user input (e.g. move forward, turn, up, down, etc.) on the server, and then setting the variables such as forward speed as replicated. Same method as shown in the Networking/Replication entry in the wiki.

Example (I skipped the validate, and fCurrentForwardSpeed has UPROPERTY with Replicated):

However, from my reading and especially Tom Shannon’s great thread on Pawn movement (which is awesome even if like me you are not doing it in blueprints) I don’t think is a very efficient way of doing it (if I’m wrong let me know).

My current thinking is to use a Custom Character Movement Component for APawn as described by Rama in the wiki. Of course he doesn’t cover replication, so I will report back how that works for me.