Sensors help the GOAP system understand the current game situation.
There are two main types of sensors: WorldSensor and TargetSensor.
Global vs. Local Sensors
Sensors can work in two modes: Global or Local.
Global: These sensors give information for all agents. For instance, IsDaytimeSensor checks if it's day or night for everyone.
Local: These sensors check only when the Planner runs. They give information for just one agent. For example, ClosestAppleSensor finds the nearest apple for a specific agent.
WorldSensor
WorldSensor checks the game's situation for an agent. It uses WorldKey to show each situation. The Planner uses this to pick the best action.
Examples:
IsHungrySensor checks if the agent is hungry.
HasAppleSensor checks if the agent has an apple.
Example
To create a new WorldSensor, create a new class that inherits from LocalWorldSensorBase or GlobalWorldSensorBase and implement its Sense method.
IsHungrySensor.cs
usingCrashKonijn.Goap.Behaviours;usingCrashKonijn.Goap.Classes;usingCrashKonijn.Goap.Classes.References;usingCrashKonijn.Goap.Sensors;usingDemos.Shared.Behaviours;namespaceDemos.Simple.Sensors.World{publicclassIsHungrySensor:LocalWorldSensorBase {publicoverridevoidCreated() { }publicoverridevoidUpdate() { }publicoverrideSenseValueSense(IMonoAgent agent,IComponentReference references) { // References are cached by the agent.var hungerBehaviour =references.GetComponent<HungerBehaviour>();if (hungerBehaviour ==null)returnfalse;returnhungerBehaviour.hunger>20; } }}
TargetSensor
TargetSensor finds a position for a TargetKey. The Planner uses this to know how far actions are.
There are two kinds of Target: TransformTarget and PositionTarget.
TransformTarget: Use this when the target can move. For example, ClosestEnemySensor finds a moving enemy.
PositionTarget: Use this for a fixed spot. Like, WanderTargetSensor finds a random spot that doesn't move.
Example
To create a new TargetSensor, create a new class that inherits from LocalTargetSensorBase or GlobalTargetSensorBase and implement its Sense method.