Toadstoolbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
The ToadstoolBrain component implements the artificial intelligence behavior tree for the Toadstool entity, a boss-type creature in Don't Starve Together. It orchestrates high-level decision-making during gameplay by sequencing actions such as channeling to spawn mushrooms, returning to its spawn point (hole), leash distance enforcement, engaging targets, fleeing after a timeout, and wandering while returning home. It depends on the KnownLocations component to store and retrieve the entity's spawn point, and integrates with the behavior tree framework (chaseandattack, leash, wander, and utility functions like GetTime and TimerExists).
Usage example
This component is typically added to an entity during its prefab definition (e.g., in a .lua file under prefabs/). Below is a minimal example of how the component might be included:
inst:AddComponent("toadstoolbrain")
The component is self-initializing: when attached, the constructor registers the behavior tree on OnStart, and the spawn point location is remembered on OnInitializationComplete.
Dependencies & tags
Components used:
knownlocations: used toGetLocation("spawnpoint")andRememberLocation("spawnpoint", pos, true)timer: used to check if a"channel"timer exists and whether a"mushroomsprout_cd"timer is present
Tags: None identified (the component itself does not add or remove tags directly).
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
timetochanneling | number? | nil | Timestamp used to track the maximum time allowed for returning to the hole before channeling. Initialized to nil in the constructor; set to GetTime() + MAX_CHANNEL_LEASH_TIME on first return check; cleared to 0 when channeling begins. |
Main functions
ToadstoolBrain:OnStart()
- Description: Constructs and assigns the root behavior tree node to
self.bt. This is the main entry point for the AI decision-making process. The behavior tree prioritizes channeling (with return-to-hole logic), leash enforcement, combat engagement, and a parallel flee/wander sequence. - Parameters: None (instance method).
- Returns:
nil. - Error states: None documented. Relies on behavior tree node constructors (
PriorityNode,SequenceNode,WhileNode,ActionNode,Leash,ChaseAndAttack,Wander,ParallelNode) which must be properly defined elsewhere.
ToadstoolBrain:OnInitializationComplete()
- Description: Records the entity’s current position as the
"spawnpoint"location usingKnownLocations:RememberLocation. Theycomponent of the position is explicitly clamped to0before storage, ensuring consistent horizontal referencing. Thedont_overwriteflag is set totrue, preventing replacement if a spawn point already exists. - Parameters: None (instance method).
- Returns:
nil. - Error states: None documented. Assumes
GetPosition()andknownlocationscomponent exist and are valid.
GetHomePos(inst) (local helper)
- Description: Retrieves the stored spawn point location from the
KnownLocationscomponent. - Parameters:
inst— The entity instance (not used directly but passed toKnownLocations:GetLocation). - Returns:
table?— The{x, y, z}position vector as stored for"spawnpoint", ornilif not yet set. - Error states: Returns
nilif"spawnpoint"has not been remembered.
ShouldChannel(self) (local helper)
- Description: Determines whether the Toadstool should begin channeling to spawn mushrooms. Conditions include: the
"channel"timer is active, or the Toadstool is engaged in combat, is below level3, and the"mushroomsprout_cd"cooldown timer is not running. - Parameters:
self— The ToadstoolBrain instance. - Returns:
boolean—trueif channeling conditions are met, otherwisefalse. Onfalse, also resetsself.timetochannelingtonil. - Error states: None documented. Uses
TimerExistsandengagedproperties; assumes those exist and are valid.
ShouldTryReturningToHole(self) (local helper)
- Description: Controls whether the Toadstool is still within the allowed return-to-hole window (
MAX_CHANNEL_LEASH_TIMEseconds =15). SetstimetochannelingtoGetTime() + MAX_CHANNEL_LESH_TIMEon first call ifnil. - Parameters:
self— The ToadstoolBrain instance. - Returns:
boolean—trueif within the time window,falseotherwise. - Error states: None documented. Uses
GetTime()and assumesMAX_CHANNEL_LEASH_TIMEis defined.
Events & listeners
- Listens to: None (no
inst:ListenForEventcalls). - Pushes:
"startchanneling"— fired by theActionNodeinside the channeling branch after returning to the hole (if applicable), triggering the channeling animation/sequence."fleewarning"— fired after aFLEE_WARNING_DELAY(3.5seconds) during the flee sequence."flee"— fired after an additionalFLEE_DELAY(10seconds), initiating the flee state.
Note: Event names are passed as string literals in
self.inst:PushEvent(...). Receivers of these events (e.g., state graphs or other components) must register handlers separately.