Carratbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
CarratBrain is a brain component responsible for controlling the behavior of the Carrat entity in Don't Starve Together. It implements a behavior tree (BT) with conditional execution flows based on the Carrat's racing state (yotc_racecompetitor.racestate) and other environmental factors (e.g., fire, enemies, food availability). It coordinates with components such as health, burnable, eater, hauntable, and yotc_racecompetitor/yotc_racestats to respond dynamically to in-game conditions. The brain integrates standard behaviors (Wander, RunAway, Panic, FaceEntity, Leash) and defines custom action triggers (e.g., eat_food_action, returntobeefalo).
Dependencies & Tags
- Components used:
health(self.inst.components.health)burnable(self.inst.components.burnable)eater(self.inst.components.eater)entitytracker(self.inst.components.entitytracker)hauntable(self.inst.components.hauntable)inventoryitem(self.inst.components.inventoryitem)yotc_racecompetitor(self.inst.components.yotc_racecompetitor)yotc_racestats(self.inst.components.yotc_racestats)
- Tags checked/avoided:
INLIMBO,outofreach,scarytoprey,character,beefalo,baby,HasCarrat,planted,Held
- Behaviors imported:
doaction,faceentity,leash,panic,runaway,wanderbehaviors/doaction,behaviors/faceentity,behaviors/leash,behaviors/panic,behaviors/runaway,behaviors/wander
- Constants:
AVOID_PLAYER_DIST,AVOID_PLAYER_DIST_SQ,AVOID_PLAYER_STOPSEE_BAIT_DIST,MAX_WANDER_DISTRACE_WANDER_DURATION,RACE_WANDER_MAX_DIST,RACE_WANDER_TIMES,RACE_WANDER_DATA
Properties
No public instance properties are explicitly declared in the constructor or elsewhere. Behavior state is managed internally via the self.bt behavior tree and helper functions.
Main Functions
CarratBrain:OnStart()
- Description: Initializes the behavior tree for the Carrat. Constructs a priority-based root node that handles states such as
OnFire,AvoidElectricFence, racing (with sub-statesprerace,postrace,raceover,racing,panic_race_start), panic from haunting, fleeing from scary entities, eating food, returning to beefalo, and wandering. The tree is assigned toself.bt. - Parameters: None.
- Returns: None. Assigns the root behavior tree to
self.bt.
Helper Functions (Internal)
racing_get_checkpoint(inst)
- Description: Returns the next race checkpoint entity if the entity is a race competitor; otherwise
nil. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
EntityInstance?— The next checkpoint entity ornil.
racing_get_checkpoint_pt(inst)
- Description: Returns the world position (Vector3) of the next race checkpoint, or
nil. Triggers the"carrat_error_walking"event if the Carrat has speed0and has not yet spoken about walking issues (tracked viawalkspeechdone). - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
Vector3?— The checkpoint position ornil.
get_race_direction(inst)
- Description: Computes a perturbed heading toward the next race checkpoint, incorporating directional variance based on the Carrat's
yotc_racestats.direction_modifier. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
number?— The calculated heading in radians, ornilif no checkpoint exists.
is_racecompetitor(inst)
- Description: Checks whether the entity has the
yotc_racecompetitorcomponent. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
boolean.
is_waiting_for_race_to_start(inst)
- Description: Checks if the Carrat is in the
"prerace"state. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
boolean.
get_trainer(inst)
- Description: Retrieves the trainer entity (via
entitytrackerkey"yotc_trainer"), if present. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
EntityInstance?— The trainer entity ornil.
get_nearby_trainer_pt(inst)
- Description: Returns the trainer's position if within
TUNING.YOTC_RACER_TRAINER_DIST; otherwisenil. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
Vector3?— The trainer's position ornil.
edible(inst, item)
- Description: Determines if
itemis edible for the Carrat based on multiple conditions (e.g.,CanEat, is bait, not held, not planted, on passable terrain, same platform). - Parameters:
inst(EntityInstance): The Carrat entity instance.item(EntityInstance): The potential food item.
- Returns:
boolean.
eat_food_action(inst)
- Description: Searches for edible bait items within
SEE_BAIT_DIST, avoiding items too close toscarytopreyentities (respectsAVOID_PLAYER_DIST). Returns a bufferedEATaction for the nearest safe food, ornilif no valid target is found. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
BufferedAction?— An action targeting an edible item ornil.
returntobeefalo(inst)
- Description: If
inst.beefalo_carratis true, searches for a nearby beefalo (excluding babies and those already carrying a Carrat), and returns aGOHOMEbuffered action. - Parameters:
inst(EntityInstance): The Carrat entity instance.
- Returns:
BufferedAction?— AGOHOMEaction targeting a beefalo ornil.
Events & Listeners
- Pushes:
"carrat_error_walking"— Triggered insideracing_get_checkpoint_ptwhen the Carrat has speed0and has not yet markedwalkspeechdoneas true. Used for speech or UI feedback in race contexts.