In this lesson, you will learn how to reduce CPU overhead in large-scale environments by strategically throttling AI perception updates and constraining sensory search radii.
When you have dozens or hundreds of AI agents, the AIPerceptionComponent can quickly become a performance bottleneck. By default, sensors often poll at high frequencies, performing expensive Line-of-Sight (LOS) traces every few frames. To optimize this, we shift from "every frame" updates to a tiered, time-sliced approach.
Throttling Update Frequencies
You should never have every agent checking for enemies simultaneously. Instead, use the PeripheralVisionAngleDegrees and AutoRegisterSources properties to filter candidates early, and implement a "staggered update" logic. In your AI Controller, you can override the perception frequency by adjusting the AISenseConfig values. For non-essential senses like hearing, increase the UpdateInterval to reduce the processing load:
// Within your AI Controller setup
UAISenseConfig_Hearing* HearingConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("HearingConfig"));
HearingConfig->DetectionByAffiliation.bDetectEnemies = true;
// Increase interval to reduce CPU spikes in large crowds
HearingConfig->UpdateInterval = 0.5f;
GetPerceptionComponent()->ConfigureSense(*HearingConfig);
Implementing Sensory Radius Limits
The cost of perception is largely tied to the number of actors within a sensing radius. To manage this in large levels, avoid using massive, global radii. Instead, implement a Distance-Based Quality Level. If an AI is in an "Idle" state, use a shorter perception radius and a lower update frequency. Only when the AI enters an "Alert" or "Combat" state do you broaden the radius and increase polling frequency.
Additionally, use UAISense_Sight::RegisterRelevantTarget sparingly. If an object isn't capable of being perceived (e.g., a static environmental prop), ensure it does not have an AIPerceptionStimuliSourceComponent attached. By limiting the number of registered stimuli sources in the world, you drastically prune the internal list the perception system must iterate through during its detection pass.
By combining larger intervals for background sensing and state-aware radius scaling, you ensure that your CPU budget is reserved for AI agents that are actively engaged with the player.
Key Takeaway: Optimize perception at scale by staggering sensor update intervals and using state-driven logic to toggle sensing radii, ensuring only high-priority agents perform frequent, high-cost calculations.