Detailed Actor Replication FlowThere is no official translation yet, so I’ll try to understand it and translate it along the way.
Introduction
Actor replication is a detailed multi-step process in which the Net Driver determines which Actors need to be replicated to which connections, and in what order. This page provides an overview of the Actor replication flow.
Most Actor replication is performed in the UNetDriver::ServerReplicateActors function. In this function, the server first collects all Actors that are determined to be relevant to each client, then sends the properties that have changed since the last update to each connected client. Next, the UActorChannel::ReplicateActor function handles the detailed operations for replicating a specific Actor to a channel.
Important Properties
There is a defined flow for how Actors are updated, when certain framework callbacks are invoked, and which properties are used to determine whether an Actor is replicated during the current server tick. The following are some important properties:
| Property | Description |
|---|---|
AActor::NetUpdateFrequency | Determines how often the Actor is replicated. |
AActor::PreReplication | Called before any replication operation occurs. |
AActor::bOnlyRelevantToOwner | If True, this Actor is only replicated to its owner. |
AActor::IsRelevancyOwnerFor | Determines Actor relevancy when bOnlyRelevantToOwner is True. |
AActor::IsNetRelevantFor | Determines Actor relevancy when bOnlyRelevantToOwner is False. |
AActor::NetDormancy | Determines whether the Actor is dormant or active. |
Actor Replication Flow Overview
The following steps form a high-level overview of the Actor replication process:
- Determine the Actors that need to be replicated, and perform checks to determine their dormancy state, update frequency, and owning connection.
- Add the Actors that pass these checks to a list ready for replication.
- Iterate over each connection and perform checks based on the current Actor and connection. After this step, you have a list of Actors to replicate for each connection.
- Sort the Actors for each connection by priority.
- Determine whether the Actor is relevant to the current connection.
- Replicate the Actor to the current connection.
The following sections provide detailed descriptions of each step in the Actor replication flow overview above.
Adding Actors to the Replication List
This step first checks all Actors to determine which Actors are replicating (by checking whether AActor::SetReplicates(true) has been called). For each replicating Actor, the NetDriver performs the following checks:
- Determine whether the current Actor starts dormant (
ENetDormancy::DORM_Initial).- If it is dormant, skip this Actor.
- Check whether the current Actor needs an update, specifically by checking the
AActor::NetUpdateFrequencyproperty.- If no update is needed, skip this Actor.
- If
AActor::bOnlyRelevantToOwneristrue, check the relevancy of the Actor’s owning connection by callingAActor::IsRelevancyOwnerFor.- If relevant, add it to that connection’s “Owner Relevant List”.
- In this case, the Actor is only sent to a single connection.
- For each Actor that passes the above checks, call
AActor::PreReplication. InAActor::PreReplication, you can decide whether certain properties should be replicated only to specific connections. You can use theDOREPLIFETIME_ACTIVE_OVERRIDEmacro to control which connections an Actor replicates to.
If the Actor passes all checks, add it to the list of Actors to replicate.
Iterating Over Each Connection
Next, the system iterates over each connection and performs the following checks and operations for each Actor in the list of Actors to replicate obtained from the previous step:
- Determine whether the current Actor is dormant by calling
AActor::NetDormancy.- If the Actor is dormant for this connection, skip this Actor.
- If the channel has not yet been opened:
- Determine whether the client has loaded the Level that contains the current Actor.
- If the Level has not been loaded, skip this Actor.
- Determine whether the current Actor is relevant to this connection by calling
AActor::IsNetRelevantFor.- If the Actor is not relevant to the connection, skip this Actor.
- Add all Actors in the connection’s “Owner Relevant List” to this list. At this point, the list contains all Actors that are relevant to this connection and are not dormant. Then sort these Actors by priority (
AActor::GetNetPriority) from highest to lowest. Sorting is critical, especially when replicating a large number of Actors, because you need to ensure that high-priority Actors are replicated first.
Iterating Over the Sorted Actor List
For each Actor in the sorted list of Actors to replicate for this connection, perform the following operations:
- If the connection has not yet loaded the Level that contains this Actor, close the channel (if it exists) and continue.
- Check once per second whether the Actor is relevant to the connection by calling
AActor::IsNetRelevantFor.- If it has been irrelevant for 5 seconds, close the channel.
- If it is relevant and no channel is open, open a channel.
- If this connection becomes saturated at any point:
- For the remaining Actors:
- If it has been relevant for less than 1 second, force an update on the next tick.
- If it has been relevant for more than 1 second, call
AActor::IsNetRelevantForto determine whether to update on the next tick.
- For the remaining Actors:
- For Actors that pass all checks, replicate the Actor to the connection by calling
UActorChannel::ReplicateActor.
Controlling the Number of Clients Replicated per Call
You can control the number of clients replicated on each call to UNetDriver::ServerReplicateActors in several ways:
Engine configuration and command-line arguments:
- Use the
-limitclienttickscommand-line argument when starting the project. - Modify the
NetClientTicksPerSecondvalue in the engine configuration (located in the[/Script/Engine.Engine]category).
- Use the
Command-line arguments:
- Use the command-line argument when starting the project:
-limitclientticks -ini:Engine:[/Script/Engine.Engine]:NetClientTicksPerSecond=<VALUE>, where<VALUE>is the number of client ticks you want to use per second.
- Use the command-line argument when starting the project:
Console variables:
- Set the
net.MaxConnectionsToTickPerServerFrameconsole variable.
- Set the
For more information, refer to UNetDriver::ServerReplicateActors_PrepConnections.
Replicating Actors to Connections
UActorChannel::ReplicateActor is the main method for replicating an Actor and all its components to a connection. Its flow is as follows:
- Determine whether this is the first update after the Actor channel is opened.
- If it is the first update, serialize the necessary information (initial location, rotation, etc.).
- Determine whether the current connection owns this Actor.
- If it does not own the Actor, and the Actor’s role is
ENetRole::ROLE_AutonomousProxy, downgrade it toENetRole::ROLE_SimulatedProxy.
- If it does not own the Actor, and the Actor’s role is
- Replicate the Actor’s changed properties.
- Replicate each component’s changed properties.
- For any deleted components, send a special deletion command.
- Once the Actor list has been processed, or the channel is saturated, start considering the next connection and repeat the process until all connections have been updated.
More Information
For more information about Actor replication, refer to the contents of the following header files:
/Engine/Source/Runtime/Engine/Classes/Engine/NetDriver.h
Information aboutUNetDriver::ServerReplicateActors./Engine/Source/Runtime/Engine/Classes/GameFramework/Actor.h
Information aboutAActorand its functions and properties./Engine/Source/Runtime/Engine/Classes/Engine/ActorChannel.h
Information aboutUActorChannelandUActorChannel::ReplicateActor./Engine/Source/Runtime/Engine/Classes/Engine/EngineTypes.h
Information about types such asENetRoleandENetDormancy.