Batbrain
Based on game build 714014 | Last updated: 2026-03-03
Overview
BatBrain implements the decision-making logic for bat entities in Don't Starve Together. It uses a Behavior Tree (BT) to manage actions such as eating, escaping from the cave, chasing targets, and interacting with team mechanics. The brain integrates with multiple components—including teamattacker, acidinfusible, eater, homeseeker, and knownlocations—to coordinate behavior based on environmental state (day/night, cave/daytime), team orders, and entity inventory.
Usage example
local inst = CreateEntity()
inst:AddComponent("brain")
inst:AddComponent("teamattacker")
inst:AddComponent("acidinfusible")
inst:AddComponent("eater")
inst:AddComponent("homeseeker")
inst:AddComponent("knownlocations")
-- ... other setup ...
inst.components.brain:SetBrainClass("batbrain")
Dependencies & tags
Components used: teamattacker, acidinfusible, eater, homeseeker, knownlocations, inventory, container, childspawner, hideout, inventoryitem
Tags: Checks for tags batdestination, player, _container, playerghost, fire, burnt, INLIMBO, outofreach, FX, NOCLICK, DECOR, inlimbo. Does not add or remove tags directly.
Properties
No public properties are defined in this brain. State is encapsulated internally within the Behavior Tree and via external component state.
Main functions
BatBrain:OnStart()
- Description: Initializes and constructs the Behavior Tree root node, incorporating panic responses, day/night navigation, team formation logic, and acid-infusion behavior.
- Parameters: None.
- Returns: Nothing.
- Error states: None; assumes required components exist. Behavior may not function correctly if key components (
teamattacker,eater,homeseeker,acidinfusible,knownlocations) are missing.
BatBrain:OnInitializationComplete()
- Description: Records the entity’s current position as the "home" location in
knownlocations, preventing overwriting if already set. - Parameters: None.
- Returns: Nothing.
GoHomeAction(inst)
- Description: Returns an
ACTION.GOHOMEaction targeting the bat’s home if valid and not in a team. - Parameters:
inst(Entity) — the bat entity. - Returns:
BufferedActionornil. - Error states: Returns
nilifhomeseekerorhomeis invalid, or if the entity is in a team.
EscapeAction(inst)
- Description: Returns an action to escape to a sinkhole (
batdestination) at night, or toGOHOMEif in cave day. - Parameters:
inst(Entity) — the bat entity. - Returns:
BufferedActionornil. - Error states: Returns
nilif no validbatdestinationis found or the destination lackschildspawner/hideout.
EatFoodAction(inst)
- Description: Prioritizes eating food from inventory; if none available, attempts to pick up edible items within range (
SEE_FOOD_DIST = 30). - Parameters:
inst(Entity) — the bat entity. - Returns:
BufferedActionornil. - Error states: Returns
nilifinventoryoreatercomponent is missing or no edible food found. Skips if state hasbusytag.
StealNitreAction(inst)
- Description: Scans nearby entities and containers for
"nitre"items to steal. Prioritizes equipped inventory (e.g., backpack) over direct slots. - Parameters:
inst(Entity) — the bat entity. - Returns:
BufferedActionwithACTIONS.STEALornil. - Error states: Returns
nilif no nitre found or ifinventoryitem.ownerisnilfor target item.
AcidBatAction(inst)
- Description: Selects an action (
EatFood,StealNitre, or fallback) based onteamattackerorders. Always leaves formation if an action is chosen. - Parameters:
inst(Entity) — the bat entity. - Returns:
BufferedActionornil. - Error states: Returns
nilif no valid action found or components (teamattacker,eater,inventory) missing.
LeaveFormation(inst)
- Description: Instructs
teamattackerto ignore formation restrictions. - Parameters:
inst(Entity) — the bat entity. - Returns: Nothing.
LeaveTeam(inst)
- Description: Instructs
teamattackerto leave the team; notifies leader if present. - Parameters:
inst(Entity) — the bat entity. - Returns: Nothing.
Events & listeners
- Listens to:
"panic"(viaEventNode) — triggers panic behavior and forces formation/team exit. - Pushes: None directly. Delegates to action system and Behavior Tree for event propagation.