In this lesson, you will learn how to optimize your GameState replication by using conditional properties and frequency throttling to reduce network congestion.
The Problem with "Always Replicate"
In a high-intensity match, your GameState often holds critical data like the RemainingTime, MatchState, or TeamScores. By default, these variables replicate every time the server ticks, which consumes valuable bandwidth. If you have 64 players in a server, sending every variable at 60Hz is unnecessary and can cause "choke," leading to lag for all your clients.
Using Conditional Replication
Conditional replication allows you to dictate when a variable should be sent over the network. Instead of constant updates, you can use Replication Conditions to restrict updates to only when data actually changes.
In C++, you define this in GetLifetimeReplicatedProps. You can use the COND_Custom or COND_SkipOwner flags to ensure that clients aren't receiving data they don't need or already have.
void AMyGameState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
// Only replicate the TeamScore if it has actually changed
DOREPLIFETIME_CONDITION(AMyGameState, TeamScore, COND_Custom);
}
Throttling Frequency
Not every piece of data requires real-time updates. The GameState is the global source of truth, but some information is "slow-moving." For example, the MatchState (Waiting, InProgress, Finished) only changes a few times per match.
You can optimize this by overriding NetUpdateFrequency in your GameState constructor. By lowering the frequency (e.g., to 2 or 5 times per second instead of the default 100), you tell the engine to check for changes much less often.
AMyGameState::AMyGameState()
{
// Lower the update frequency because GameState doesn't need
// high-precision, frame-by-frame updates.
NetUpdateFrequency = 5.0f;
}
By combining these techniques, you ensure that the server only pushes global updates when absolutely necessary, leaving more bandwidth available for high-priority data like player movement and combat interactions.
Key Takeaway: Use lower NetUpdateFrequency for global state and apply Replication Conditions to ensure data is only sent when it changes, keeping your network traffic lean during high-intensity gameplay.