In this lesson, you will learn how to inject situational context into your Utility AI system by applying dynamic multipliers to base action scores.
While a static Utility AI might assign a flat score to a "Flee" action based solely on health, real gameplay is more nuanced. An NPC at 30% health should be significantly more desperate if an enemy is standing three feet away than if they are safe in a back room. To achieve this, we introduce Dynamic Modifiers that act as coefficients to your base utility scores.
The Modifier Pattern
Think of a modifier as a "contextual filter." After calculating the raw utility score for an action, you pass that value through a chain of modifier functions. Each modifier evaluates a specific world state—such as current health or local threat density—and returns a multiplier (typically between 0.0 and 2.0).
public float CalculateModifiedUtility(Action action, AgentContext context) {
float score = action.GetBaseUtility(context);
// Apply situational modifiers
score *= GetHealthMultiplier(context.Health);
score *= GetThreatMultiplier(context.NearbyEnemies);
return Mathf.Clamp01(score);
}
Implementing Contextual Logic
A GetThreatMultiplier function should look at your SpatialMemorySystem to count detected enemies within a radius. If the count is high, the multiplier for "Flee" or "Take Cover" should scale upward, while the multiplier for "Attack" might scale downward if the agent is overwhelmed.
By separating the base desire (what the NPC wants to do) from the situational modifier (what the current environment demands), you decouple your logic. This allows you to tune combat behavior without rewriting the core scoring math. If your NPC feels too brave, you simply adjust the response curve of the ThreatMultiplier rather than touching the base action weights.
This approach ensures that your NPC's decision-making reflects the chaotic reality of the game world, transforming stiff, predictable behaviors into reactive, intelligent-feeling agents.
Key Takeaway: By multiplying base utility scores by dynamic, context-aware coefficients, you allow your AI to prioritize behaviors based on immediate environmental pressure rather than fixed preferences.