Avoidelectricfence
Based on game build 714014 | Last updated: 2026-03-03
Overview
AvoidElectricFence is a behavior node used in AI brain logic to trigger evasive movement when an entity comes into contact with or is near an electric fence. It calculates a safe run angle away from the fence(s) and commands the entity to move using the locomotor component. If the entity has a combat component, it drops any active target before fleeing.
This component integrates with the BrainCommon.HasElectricFencePanicTriggerNode mechanism by setting an internal tag on the entity, and responds to two key events: startelectrocute and shocked_by_new_field.
Usage example
-- Typically added to prefabs via brain definition:
-- brain:InsertChild(AvoidElectricFence(inst))
-- The component does not need manual addition; it is instantiated and managed by the brain tree.
-- Internally, it registers event callbacks and uses `Combat:DropTarget()` and `Locomotor:RunInDirection()` when triggered.
Dependencies & tags
Components used: combat, locomotor
Tags: Sets inst._has_electric_fence_panic_trigger = true (private/internal flag for brain node detection)
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | nil | Reference to the entity instance the behavior belongs to. |
run_angle | number | nil | Cached direction (in degrees, 0 <= x < 360) to flee; set when shocked_by_new_field fires. |
shocked_by_field | function | nil | Event handler for "shocked_by_new_field"; computes and caches run_angle. |
Main functions
GetRunAngle(field)
- Description: Calculates the average direction away from all fences in the given electric fence
field. It sums unit vectors pointing from the entity to each fence, then inverts the result and normalizes viaatan2. - Parameters:
field(table) — an object containing afencesarray, where each fence has aTransformcomponent accessible viafield.fences[i].Transform. - Returns:
number— a flee angle in degrees, normalized to the range[0, 360). - Error states: Returns
0if the field has no fences (emptyfencesarray), though no explicit guard exists in the current implementation.
Visit()
- Description: The core behavior execution method. When the node is active (
status == READY), it triggers fleeing behavior:
- Drops the entity’s combat target (via
Combat:DropTarget()), if present. - Initiates movement in the saved
run_angledirection (viaLocomotor:RunInDirection()).
- Parameters: None.
- Returns: Nothing.
- Error states: No-op if
self.run_angleisnil, or if thelocomotorcomponent is missing.
OnStop()
- Description: Cleans up event listeners when the behavior node is terminated.
- Parameters: None.
- Returns: Nothing.
Events & listeners
- Listens to:
startelectrocute— triggersBrain:ForceUpdate()to re-evaluate the node state (via the internalonelectrocutecallback).
shocked_by_new_field— triggersself.shocked_by_field, which computesrun_angleand forces a brain update. - Pushes: None (this component does not fire custom events).