In this lesson, you will learn how to trigger complex client-side logic automatically the moment a replicated variable arrives from the server using RepNotify functions.
The Power of RepNotify
When you replicate a variable, the server updates the client’s copy, but the client doesn't inherently know when that update happened. If you simply poll the variable in Tick, you waste performance. Instead, we use RepNotify. By setting a variable's replication condition to RepNotify, Unreal Engine automatically executes a specific function on the client whenever the property value is updated by the server.
Implementation Strategy
This pattern is ideal for visual triggers—like updating a health bar UI or toggling a glow effect on an object—because it ensures the logic only fires when the underlying data actually changes, rather than every frame.
Consider a "Power Up" state on a networked object:
// In your Header File
UPROPERTY(ReplicatedUsing = OnRep_IsPowered)
bool bIsPowered;
UFUNCTION()
void OnRep_IsPowered();
// In your Source File
void AMyActor::OnRep_IsPowered()
{
// This executes only on clients when the server updates bIsPowered
if (bIsPowered) {
MeshComponent->SetMaterial(0, PoweredMaterial);
ActivateParticleSystem();
} else {
MeshComponent->SetMaterial(0, DefaultMaterial);
DeactivateParticleSystem();
}
}
Critical Architectural Note
A common pitfall for advanced developers is forgetting that OnRep functions do not execute on the server by default. If your logic needs to run on both the server (to handle gameplay rules) and the client (to handle visual feedback), you must manually call the OnRep function from within the server-side code that updates the variable.
Always encapsulate the "state update" logic within the OnRep function itself. This creates a single, reliable point of entry for your logic, preventing discrepancies between what the server thinks is happening and what the client is actually rendering. By relying on RepNotify, you keep your networking logic clean, efficient, and decoupled from the expensive main game loop.
Key Takeaway: RepNotify functions provide a high-performance, event-driven callback mechanism that ensures client-side logic remains perfectly synchronized with server-side state changes.