Course contentsShow
← Unreal Engine Game Development
Lesson 752 of 88010. Networking and ReplicationFree lesson

Creating Custom Network Relevancy Predicates

Override IsNetRelevantFor to implement custom distance-based or logic-based checks that selectively disable replication for actors hidden from specific players.

In this lesson, you will learn how to override IsNetRelevantFor to gain granular control over which players receive updates for specific actors, effectively reducing bandwidth by ignoring irrelevant network traffic.

The Logic of Network Relevancy

By default, Unreal Engine determines if an actor is "relevant" based on distance from the camera, ownership, and net priority. However, in complex scenarios—such as a stealth game where guards shouldn't "know" about players behind solid walls, or a tactical shooter where you want to cull distant snipers—the default logic isn't enough.

The virtual bool IsNetRelevantFor(...) function is the gatekeeper of the replication system. It is called by the UNetDriver on the server for every connected client. If it returns false, the server will not replicate any property changes or RPCs to that specific client for that frame.

Implementing Custom Predicates

To create a custom predicate, you override this function in your AActor subclass. You can inject custom game logic, such as a Line-of-Sight (LOS) check or a "team-only" visibility filter, before calling the base implementation to ensure you don't break engine-level requirements.

bool AMyTacticalActor::IsNetRelevantFor(const AActor* RealViewer, const AActor* ViewTarget, const FVector& SrcLocation) const
{
    // 1. Always allow if the actor is owned by the viewer
    if (GetOwner() == RealViewer) return true;

    // 2. Custom Logic: Only replicate if the viewer is on the same team
    if (MyTeamID != Cast<AMyPlayerController>(RealViewer->GetController())->GetTeamID())
    {
        return false; 
    }

    // 3. Fall back to standard distance-based relevancy checks
    return Super::IsNetRelevantFor(RealViewer, ViewTarget, SrcLocation);
}

Why Use Custom Predicates?

When you return false, you aren't just saving CPU on the server; you are preventing the packet from ever being sent. This significantly reduces the network load for the server and lowers the client's memory overhead by preventing the client from tracking objects it shouldn't even know exist. Always ensure your custom logic is performant, as this function is evaluated frequently for many actors during gameplay.

Key Takeaway: Overriding IsNetRelevantFor allows you to inject custom gameplay rules to surgically prune the network graph, ensuring only necessary data reaches the client.