Smallghostbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
smallghostbrain defines the behavior tree for small ghost entities in Don't Starve Together. It orchestrates core ghost behaviors: following a leader, searching for lost toys (especially those dropped near the leader), providing hints via jump-in actions to guide the leader toward nearby toys, and retreating when combat becomes too dangerous. It uses custom action functions and a hierarchical behavior tree built with PriorityNode, WhileNode, SequenceNode, and decorator nodes.
This brain is tightly coupled with the follower, health, grouptargeter, knownlocations, talker, and combat components. It makes decisions based on relative positions, time-based cooldowns, and state flags stored in the stategraph's memory (inst.sg.mem).
Usage example
The brain is instantiated as a component and attached to a small ghost entity via the brain system. It is not intended to be manually added or modified in mod code. A typical instantiation pattern (handled internally) looks like:
inst:AddComponent("brain")
inst.components.brain:SetBrainClass("smallghostbrain")
No direct method calls are required; the brain activates automatically on stategraph start via OnStart().
Dependencies & tags
Components used:
follower(viaGetLeader())health(to check if targets are dead)grouptargeter(to verify targeting relationship)knownlocations(to retrieve home location)talker(to trigger speech events)combat(implicitly via combat avoidance logic)
Tags:
- None added or removed by this script.
- Uses tags for filtering in combat avoidance:
"_combat"and"_health"(allowed),"wall"and"INLIMBO"(excluded).
Properties
No public properties are initialized or exposed by the constructor itself. Internal state is managed via instance variables on self and inst, including:
inst._toys: Likely populated externally (e.g., by a parenttoy_ownercomponent) for use inget_closest_toy().inst.sg.mem.is_hinting: Boolean flag set during hinting.inst._has_done_speech: Boolean flag preventing repeated speech on toy search triggers.inst._next_leader_toy_search_speech_time: Timestamp for speech cooldown.
Main functions
The core logic resides in helper functions called from behavior tree nodes. The brain itself is implemented as a class with a single entrypoint, OnStart, which constructs the behavior tree.
SmallGhostBrain:OnStart()
- Description: Initializes the behavior tree on stategraph start. Constructs a priority-ordered root node tree implementing ghost gameplay loops: toy retrieval, combat avoidance, hinting, searching, and idle wandering.
- Parameters: None (instance method).
- Returns: Nothing. Assigns
self.btto a newBTinstance with the constructed root tree. - Error states: None documented.
get_closest_toy(toy_owner, dist_inst, dsq_gate)
- Description: Finds the closest toy owned by
toy_ownertodist_inst, optionally bounded by a squared distance threshold (dsq_gate). Used by hint, pickup, and search logic. - Parameters:
toy_owner: Entity that owns a_toystable of toy entities.dist_inst: Reference entity for distance measurement.dsq_gate: Optional squared distance threshold (number). If provided, only toys within this distance are considered.
- Returns:
toyentity (Inst), ornilif no eligible toys exist ortoy_owner._toysis empty/nil. - Error states: Returns
niliftoy_owner._toysis nil or empty, or ifdist_instis nil.
pickup_lost_toy(inst)
- Description: Attempts to pick up the closest lost toy within
TUNING.GHOST_HUNT.PICKUP_DSQ. Invoked by aDoActionnode during the behavior tree root priority check. - Parameters:
inst: The ghost entity instance.
- Returns: A
BufferedActiontoward the toy andACTIONS.PICKUP, ornilif the ghost is busy or no toy is nearby. - Error states: Returns
nilif the ghost’s stategraph has the"busy"tag.
get_hint_location(inst)
- Description: Computes a jump-in hint position near the leader and the closest toy, if the leader is at an appropriate distance to benefit from a hint. Marks
inst.sg.mem.is_hinting = truewhen hinting starts. - Parameters:
inst: The ghost entity instance.
- Returns: A
BufferedActionwithACTIONS.JUMPINto a position offset from the closest toy toward the leader (with randomness), ornilif no leader, no toy, or leader-toy distance is too close/far. - Error states: Returns
nilif leader or closest toy is missing, or if the leader-toy distance lies outside[MIN_HINT_DSQ, MAX_HINT_DSQ].
test_for_finished_hinting(inst)
- Description: Evaluates whether hinting should end (e.g., due to reaching the leader, toy being lost, or leader moving too far). Clears
inst.sg.mem.is_hintingwhen appropriate. - Parameters:
inst: The ghost entity instance.
- Returns: Nothing (side-effect only).
- Error states: None; always safely exits if
inst.sg.mem.is_hintingis false or missing.
test_for_toy_in_search_range(inst)
- Description: Checks if any toy is within the minimum hint distance of the leader, indicating the ghost should enter search mode.
- Parameters:
inst: The ghost entity instance.
- Returns:
trueif a toy is withinMIN_HINT_DSQof the leader; otherwisefalse. - Error states: Returns
falseif leader or toys are missing.
try_toy_search_speech(inst)
- Description: Triggers a randomized speech line and hints the leader when a toy search begins, respecting a per-search cooldown.
- Parameters:
inst: The ghost entity instance.
- Returns: Nothing (side-effect only via
talker:Sayand scheduled delayed action). - Error states: Skips if
inst._has_done_speechistrue, or ifleader.components.talkeris absent.
_avoidtargetfn(self, target)
- Description: Core predicate used to determine if
self.instshould avoid a given combat participant (target). Considers leader proximity, combat activity timeouts, and targeting relationships. - Parameters:
self: The brain instance (required for closure access toself.inst).target: Potential enemy entity to avoid.
- Returns:
trueifself.instshould run away fromtarget;falseotherwise. - Error states: Returns
falseiftargetis invalid, lackscombat/health, or is too far to pose a threat.
validate_combat_avoidance(self)
- Description: Validates that the currently tracked
runawayfromentity remains a valid combat avoidance target. Resetsself.runawayfromtonilif conditions are no longer met. - Parameters:
self: The brain instance.
- Returns:
trueifrunawayfromremains valid and should continue avoidance;falseotherwise. - Error states: Resets
self.runawayfromtonilon exit if validation fails.
toy_nearby_wander_home(inst)
- Description: Helper for
Wanderbehavior when a toy is nearby — returns the leader’s position as wander destination (if available). - Parameters:
inst: The ghost entity instance.
- Returns:
leader:GetPosition()ornilif leader is missing.
toy_nearby_wander_angle(inst)
- Description: Helper for
Wanderbehavior — returns a randomized angle around the line from the closest toy to the leader. - Parameters:
inst: The ghost entity instance.
- Returns: Random angle (number) or
TWOPI * math.random()for fully random direction if no toy/leader.
KeepFacingTarget(inst, target)
- Description: Condition for the
FaceEntitybehavior during combat avoidance — ensures the ghost faces its leader only if the target matches. - Parameters:
inst: The ghost entity instance.target: The entity being faced.
- Returns:
trueifGetLeader(inst) == target;falseotherwise.
Events & listeners
This script does not register or fire any events directly via inst:ListenForEvent or inst:PushEvent. All state transitions and responses are managed through the behavior tree execution and stategraph memory (inst.sg.mem).