Approach
Overview
The Approach component is a behaviour node used within DST's behaviour tree system to programmatically move an entity toward a target entity. It calculates the distance to the target and controls locomotion via the locomotor component, optionally allowing running or platform-hopping based on state and configuration. It is typically used for AI pathfinding in scenarios such as chasing enemies, following allies, or navigating terrain with platform constraints. The component integrates with the health component to gracefully abort movement if the target dies during navigation.
Dependencies & Tags
- Components used:
locomotor: Used for movement (GoToPoint,Stop) and platform-hopping logic (allow_platform_hoppingproperty).health: Checked forIsDead()state to determine if the target is still valid.
- Tags: None identified.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | The entity instance that owns this behaviour component. |
target | Entity or function | — | The target entity, or a function returning a target entity (resolved via FunctionOrValue). |
dist | number or nil | nil | Static squared distance threshold (converted from dist^2). If dist is provided as a function, self.dist_fn stores it instead. |
dist_fn | function or nil | nil | A callable function that returns the dynamic distance threshold. Called in EvaluateDistances(). |
canrun | boolean | true | Whether the entity is allowed to run (high-speed movement) when approaching. |
currenttarget | Entity or nil | nil | Cache of the currently active target, resolved during the READY phase. |
Main Functions
GetTarget()
- Description: Resolves and validates the stored
target. Iftargetis a function, it is invoked withself.instas an argument. Returns the target entity only if valid and non-nil; otherwise returnsnil. - Parameters: None.
- Returns:
Entity?— The validated target entity, ornilif no valid target exists.
EvaluateDistances()
- Description: Recomputes the
distthreshold ifdist_fnis set. This is called when the target changes (to ensure fresh distance criteria) during theVisit()logic. - Parameters: None.
- Returns:
nil— Updatesself.distin-place.
DBString()
- Description: Provides a debug-friendly string representation of the current state, including the resolved target and the Euclidean distance to it.
- Parameters: None.
- Returns:
string— Formatted string like"Entity(123), (5.42) ".
AreDifferentPlatforms(inst, target)
- Description: Checks if
instandtargetoccupy different terrain platforms (e.g., ground vs. floating island). Respects theallow_platform_hoppingsetting in thelocomotorcomponent. - Parameters:
inst(Entity) — The moving entity.target(Entity) — The target entity.
- Returns:
boolean—trueif platforms differ and platform-hopping is disabled; otherwisefalse.
Visit()
- Description: Core state machine logic for the behaviour node. Handles transition from
READYtoRUNNING(initiating movement) orSUCCESS(distance threshold met), and updates movement based on target validity, distance, and platform state. Re-evaluates every ~0.25 seconds viaSleep(). - Parameters: None.
- Returns:
nil— Modifiesself.statusand callslocomotormethods.
Events & Listeners
None. The Approach component does not register or fire events directly; it interacts via direct method calls and state tracking.