Memory & Awareness
Vision and hearing answer "what do I perceive right now?". Decisions need more: "I saw someone by the crates four seconds ago", "I'm fairly sure something is wrong", "it's been quiet for a minute, back to my rounds". That is the agent's memory, and it is what separates a guard that forgets you the instant you step behind a pillar from one that walks over to check.
Three ideas cover most games:
| Idea | What it stores | Used for |
|---|---|---|
| Awareness (suspicion) meter | one number per agent, 0–100 | gradual detection, the "?" → "!" icons, fair stealth |
| Last-known position | where and when a target was last perceived | investigate, search, chase around corners |
| Memory records | per target: position, time, sense, confidence | choosing targets, forgetting, sharing with a squad |
A suspicion meter with states
The meter rises while the guard perceives something (faster when the player is close or the stimulus is strong) and decays when nothing happens, but only after a short grace period, so a glimpse followed by silence still leaves the guard uneasy for a while. Thresholds turn the number into states that the decision layer (an FSM or a behavior tree) can react to.
The program feeds a scripted timeline of stimuli into the meter, one tick every 0.5 s:
using System;
var mind = new Awareness();
// tick -> what the guard perceives this tick (null = nothing)
var timeline = new (int Tick, Stimulus S)[]
{
(2, new Stimulus("glimpse", 0.3, new V2(14, 3))),
(3, new Stimulus("glimpse", 0.3, new V2(13, 3))),
(16, new Stimulus("footstep", 1.0, new V2(9, 5))),
(20, new Stimulus("seen", 1.0, new V2(7, 5))),
(21, new Stimulus("seen", 1.0, new V2(6, 6))),
(22, new Stimulus("seen", 1.0, new V2(5, 6))),
};
for (int tick = 0; tick <= 28; tick++)
{
Stimulus? s = null;
foreach (var e in timeline) if (e.Tick == tick) s = e.S;
string before = mind.State;
mind.Update(s, 0.5);
bool changed = mind.State != before;
if (s != null || changed || tick % 8 == 0)
Console.WriteLine(quot;t={tick * 0.5,4:F1}s {(s?.Kind ?? "-"),-8} suspicion {mind.Suspicion,5:F1} " +
quot;{mind.State,-10} last known {mind.LastKnown?.ToString() ?? "none"}" +
(changed ? quot; <- {before} to {mind.State}" : ""));
}
record Stimulus(string Kind, double Strength, V2 Pos);
class Awareness
{
public double Suspicion; // 0..100
public V2? LastKnown;
double sinceStimulus = 999;
const double RiseRate = 60, DecayRate = 3, Grace = 2; // per second, per second, seconds
public string State => Suspicion >= 100 ? "Alert" : Suspicion >= 30 ? "Searching" : Suspicion > 0 ? "Suspicious" : "Unaware";
public void Update(Stimulus? s, double dt)
{
if (s != null)
{
Suspicion = Math.Min(100, Suspicion + RiseRate * s.Strength * dt);
LastKnown = s.Pos;
sinceStimulus = 0;
return;
}
sinceStimulus += dt;
if (sinceStimulus > Grace && Suspicion < 100) // Alert sticks until the chase logic resets it
Suspicion = Math.Max(0, Suspicion - DecayRate * dt);
if (Suspicion == 0) LastKnown = null; // forgotten
}
}
readonly record struct V2(double X, double Y)
{
public override string ToString() => quot;({X:F0}, {Y:F0})";
}t= 0.0s - suspicion 0.0 Unaware last known none
t= 1.0s glimpse suspicion 9.0 Suspicious last known (14, 3) <- Unaware to Suspicious
t= 1.5s glimpse suspicion 18.0 Suspicious last known (13, 3)
t= 4.0s - suspicion 16.5 Suspicious last known (13, 3)
t= 8.0s footstep suspicion 36.0 Searching last known (9, 5) <- Suspicious to Searching
t=10.0s seen suspicion 66.0 Searching last known (7, 5)
t=10.5s seen suspicion 96.0 Searching last known (6, 6)
t=11.0s seen suspicion 100.0 Alert last known (5, 6) <- Searching to Alert
t=12.0s - suspicion 100.0 Alert last known (5, 6)
Read the timeline the way a player would experience it: two glimpses make the guard Suspicious (a "?" over its head); after the grace period the meter slowly drains; a footstep pushes it back up and the guard starts Searching the last-known position; three ticks of clear sight push it to Alert. The last-known position updates with every stimulus, so the guard always searches the latest place it had evidence for.
Note: Alert doesn't decay in this model. Real games leave Alert through the decision layer: the chase loses the player, the guard searches the last-known position for a while, then drops to Searching with the meter reset to, say, 60. Lowering and remembering ("they were here, they might come back") is what makes guards feel smart after an escape.
Memory records per target
When an agent can perceive several things (players, allies, thrown objects, corpses), keep one record per target and let records age:
record: target, lastPosition, lastVelocity, lastSeenTime, sense, confidence
confidence decays with age; the record is dropped when it falls below a thresholdThe decision layer then asks memory questions instead of perception questions: "who is my most recent, most confident hostile?", "has anyone seen the player in the last 10 seconds?". Storing the last velocity lets the AI predict where a fleeing player went (like pursuit) and search there, which feels much smarter than walking to the exact last spot.
Design rules that keep memory fair
- Never read the player's real position when the agent doesn't perceive them. Decisions must use the memory, or the AI will "cheat" in ways players notice instantly (see Field Notes: Design).
- Show the state. The ? and ! icons, a rising meter, a voice line ("Must have been rats…") turn hidden numbers into gameplay.
- Tune rise and decay rates per difficulty instead of changing vision range: it's easier to balance and easier for players to read.
- Share memories deliberately. A guard that radios "intruder at the east gate" writes a record into the squad's shared blackboard; one that just sees doesn't magically inform everyone.
Engines provide parts of this: Unreal's perception component keeps stimuli with an age and a max age per sense, and blackboards store last-known positions. The thresholds and the reactions are always game code.
Test yourself
Why does the suspicion meter wait for a grace period before decaying?
- To save CPU time
- So a brief glimpse followed by silence still leaves the guard uneasy for a while
- Because decay is not allowed in Alert
- To make detection instant
What should the AI use to decide where to search after losing sight of the player?
- The player's real current position
- Its last-known position (and possibly last velocity) from memory
- A random point
- The level's spawn point
In the program, what moves the guard from Searching to Alert?
- The footstep
- Several ticks of clear sight with strength 1.0
- The grace period running out
- The meter decaying to 0