Perdbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
The PerdBrain component defines the behavior tree for the Perd character in Don't Starve Together. As a specialized AI brain, it orchestrates high-level decision-making processes including seeking food, picking berries, returning to a home base (typically a bush), and participating in special events by seeking and loitering near Perd shrines. It leverages the Behavior Tree (BT) system and integrates closely with components like inventory, eater, pickable, burnable, and homeseeker to determine valid targets and actions. The brain responds to environmental conditions (e.g., day/night, fire hazards, proximity to threats) to drive dynamic and context-aware behavior.
Usage example
The component is instantiated automatically for Perd entities and registered via the brain system—there is no direct manual instantiation in mod code. However, an example of enabling shrine seeking and verifying behavior follows:
-- Assuming `perd` is an entity instance with PerdBrain attached
perd.seekshrine = true
perd.components.homeseeker.home = some_bush_inst
-- The brain will now prioritize shrine-seeking behavior when active
perd:PushEvent("startbrain")
Dependencies & tags
Components used:
burnable(IsBurning)eater(CanEat)homeseeker(homeproperty)inventory(FindItem)inventoryitem(ownerproperty)pickable(CanBePicked,productproperty)
Tags:
- Internal tag groups used for entity filtering:
bush,edible_veggie,edible_meat,edible_fish,edible_insects,edible_fungi(viaFOODTYPEconstants),scarytoprey,perdshrine,burnt,fire,pickable,INLIMBO,outofreach.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst.seekshrine | boolean | false | Controls whether the brain prioritizes seeking and loitering near Perd shrines. |
inst._shrine | entity or nil | nil | Cached reference to the nearest valid Perd shrine (only populated when seekshrine is true and a shrine is found). |
inst._lastshrinewandertime | number or nil | nil | Timestamp of the last shrine-related wander; used to calculate loiter duration. |
inst._shrine (local in FindShrine) | entity or nil | nil | Internal cache used by FindShrine to avoid repeated entity searches. |
Main functions
FindNearestBush(inst)
- Description: Scans the environment for the nearest bush within
SEE_BUSH_DISTthat can be picked (i.e., is not burnt, is visible, andCanBePickedreturnstrue). Falls back to returning the home bush (homeseeker.home) if no functional bush is found. - Parameters:
inst— the entity instance for which to find a bush (typically the Perd). - Returns: A valid bush entity or
nilif no suitable bush is found and no home is set. - Error states: May return a bush that cannot be picked if no fully functional bush is found and
homeseeker.homeis valid.
HomePos(inst)
- Description: Helper that returns the position of the nearest or preferred bush (via
FindNearestBush) for homing behavior. - Parameters:
inst— the entity instance. - Returns: A
Vector3position ornilif no home bush can be determined. - Error states: Returns
nilifFindNearestBushreturnsnil.
GoHomeAction(inst)
- Description: Constructs a buffered "Go Home" action targeting the nearest bush if available.
- Parameters:
inst— the entity instance. - Returns: A
BufferedActionornilif no bush is found. - Error states: Returns
nilifFindNearestBushfails to locate a target.
EatFoodAction(inst, checksafety)
- Description: Attempts to locate and return an action to eat edible food. First checks inventory for edible items; if none are found, searches the world for food. Optionally validates safety (checks for
scarytopreyand water/invalid ground). - Parameters:
inst— the entity instance.checksafety(boolean) — iftrue, ensures no scary entities are withinSEE_PLAYER_DISTand that food is on valid ground.
- Returns: A
BufferedActionfor eating ornilif no edible food is found or safety conditions fail. - Error states: Returns
nilifinst.components.inventoryorinst.components.eaterare missing, no suitable food is found, or safety checks fail.
EatFoodWhenSafe(inst)
- Description: Calls
EatFoodAction(inst, true)to return an eat action only when safety is confirmed. - Parameters:
inst— the entity instance. - Returns: A
BufferedActionornil.
EatFoodAnytime(inst)
- Description: Calls
EatFoodAction(inst, false)to return an eat action regardless of nearby threats. - Parameters:
inst— the entity instance. - Returns: A
BufferedActionornil.
PickBerriesAction(inst)
- Description: Locates a nearby berry-producing bush (via
HasBerry) and returns an action to pick berries, only if noscarytopreyentities are nearby. - Parameters:
inst— the entity instance. - Returns: A
BufferedActionfor picking ornilif no valid bush is found or safety conditions fail. - Error states: Returns
nilifFindEntityreturns no matches orGetClosestInstWithTagfinds a threat.
FindShrine(inst)
- Description: Locates and caches a valid Perd shrine within
SEE_SHRINE_DIST, skipping burnt or burning shrines. Resets the shrine cache ifseekshrineisfalse. - Parameters:
inst— the entity instance. - Returns: A shrine entity or
nil. - Error states: Returns
nilif no valid shrine is found,seekshrineisfalse, or the cached shrine is invalid/burnt/out-of-range.
ShrinePos(inst)
- Description: Returns the world position of the cached shrine.
- Parameters:
inst— the entity instance. - Returns: A
Vector3position (assumes shrine is valid). - Error states: May throw an error if
inst._shrineisnilat call time.
ShrineWanderPos(inst)
- Description: Calculates a wander point at a safe distance (
MIN_SHRINE_WANDER_DIST) from the shrine center for loitering behavior. - Parameters:
inst— the entity instance. - Returns: A
Vector3position. - Error states: May produce undefined behavior if shrine position is invalid.
ShouldLoiter(inst)
- Description: Determines whether Perd should loiter near the shrine based on elapsed time since last wander and randomization.
- Parameters:
inst— the entity instance. - Returns:
trueif loitering should occur,falseotherwise. - Error states: May incorrectly return
trueduring initial shrine approach ifinst._lastshrinewandertimeis unset.
OnStart()
- Description: Initializes the Perd behavior tree root node with priority-ordered subtrees handling: panic, night homing, shrine seeking (if enabled), safe eating, escaping threats, berry picking, and general wandering.
- Parameters: None.
- Returns: None. Sets
self.btto aBTinstance with aPriorityNoderoot. - Error states: Behavior may stall if required actions return
nilwithout proper fallbacks.
Events & listeners
This component does not register event listeners via inst:ListenForEvent. It integrates with the brain system via OnStart, which is called when the brain is activated. Events like "panic", "beattacked", or "onupdate" are handled internally by the behavior tree nodes (e.g., BrainCommon.PanicTrigger, RunAway, WhileNode) rather than explicit listeners.