Standandattack
Based on game build 714014 | Last updated: 2026-03-03
Overview
StandAndAttack is a behaviour tree node used to implement stationary combat AI. It manages the logic for selecting a target, issuing attacks, and exiting the state under conditions such as target death, timeout, or loss of target validity. It integrates with the combat, health, and locomotor components, and can optionally stop locomotion before each attack attempt.
Usage example
local inst = CreateEntity()
inst:AddComponent("combat")
inst:AddComponent("health")
inst:AddComponent("locomotor")
-- Create a FindNewTargetFn (e.g., nearest living enemy)
local function findTarget(e)
return FindEntity(e, 10, nil, { "combat", "heart" })
end
-- Add the StandAndAttack behaviour node
inst:AddTag("standandattack")
inst.behaviourtree:AddNode("standandattack", StandAndAttack(inst, findTarget, 10, true))
Dependencies & tags
Components used: combat, health, locomotor
Tags: Checks canrotate; no tags added or removed by this component.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | nil | The entity instance this behaviour controls. |
findnewtargetfn | function? | nil | Optional callback to find a new target if none is set. |
timeout | number? | nil | Maximum time (seconds) to remain in this state before failing. |
shouldstoplocomotor | boolean? | nil | If true, stops locomotion before each attack attempt. |
numattacks | number | 0 | Counter for total attacks/misses recorded since activation. |
starttime | number? | nil | Timestamp when the state entered RUNNING. |
Main functions
Visit()
- Description: Core logic for the behaviour node. Evaluates target state, updates timeout, handles attacks, and transitions between
READY,RUNNING,SUCCESS, andFAILEDstates. - Parameters: None.
- Returns: Nothing (state transitions modify
self.statusinternally). - Error states: Returns early if state is neither
READYnorRUNNING. Fails ifcombat.targetis invalid or missing, timeout exceeded, or target becomes dead.
OnAttackOther(target)
- Description: Callback triggered by
onattackotherandonmissotherevents. Incrementsnumattacksand resets the timeout timer (starttime). - Parameters:
target(Entity?) — the entity that was attacked or missed. - Returns: Nothing.
OnStop()
- Description: Cleans up event listeners when the behaviour node is removed or terminated.
- Parameters: None.
- Returns: Nothing.
Events & listeners
- Listens to:
onattackother— triggersOnAttackOtherto count attacks. - Listens to:
onmissother— also triggersOnAttackOtherto count misses. - Pushes: No events directly; relies on
combat:TryAttack()and internal state changes for side effects.