Sharkbrain
Based on game build 714014 | Last updated: 2026-02-27
Overview
SharkBrain is a brain component that implements behavior tree logic for sharks in Don't Starve Together. It governs their movement patterns, aggression, and feeding strategies—including the unique ability to follow boats on the surface of the ocean. The brain prioritizes survival behaviors (e.g., retreating from danger or staying in water) and dynamically chooses actions like wandering, chasing targets, attacking, or seeking food. It relies heavily on external components such as combat (for target tracking), eater (for food validation), homeseeker (for home position), timer (for cooldowns), and boatphysics (for boat velocity and follow position calculations).
Usage example
The component is instantiated automatically by the game when a shark entity is created and is not typically added manually by modders.
-- Inside a shark prefab file (e.g., prefabs/shark.lua):
inst:AddComponent("brain")
inst.components.brain:SetBrain("sharkbrain")
Dependencies & tags
Components used: combat, eater, homeseeker, timer, boatphysics, inventoryitem (indirectly via IsHeld check).
Tags: Checks for "INLIMBO" and "outofreach" tags in entity search filters; no tags are added/removed dynamically by this brain itself.
Properties
The component does not declare any public properties in its constructor. Behavior is entirely driven by local functions and the behavior tree set up in OnStart(). Only internal state variables are used temporarily during behavior execution (e.g., self.inst.targetboat, self.inst.foodtoeat, self._removefood), which are not persistent or serialized.
Main functions
OnStart()
- Description: Constructs and initializes the behavior tree root node. Sets up conditional priorities for behaviors based on environment (water vs. land), proximity to threats (via
PanicTrigger/ElectricFencePanicTrigger), and other game state. Behaviors such asWander,ChaseAndAttack,DoAction(isfoodnearby), andDoAction(EatFishAction)are included in a priority-ordered list under the"on water"condition. Attack behavior only triggers when the shark is not in water and not currently in a"jumping"state. The behavior tree is stored inself.bt. - Parameters: None.
- Returns: None.
- Error states: None documented. Assumes proper initialization of the base
Brainclass.
isOnWater(inst)
- Description: Helper that determines if the entity is currently in water by checking if it has no platform (
GetCurrentPlatformreturnsnil) and the underlying world is not solid ground. - Parameters:
inst(Entity instance). - Returns:
trueif the entity is floating on water; otherwisefalse. - Error states: Uses
TheWorld.Map:IsVisualGroundAtPoint(...), which may behave unpredictably near cliffs or map edges.
GetHome(inst)
- Description: Retrieves the home entity via
homeseeker.homeif the component exists. - Parameters:
inst(Entity instance). - Returns:
inst.components.homeseeker.homeifhomeseekeris attached, otherwisenil.
GetHomePos(inst)
- Description: Gets the world position of the home entity.
- Parameters:
inst(Entity instance). - Returns: World position vector (
Vector3) ornilif no home is set.
GetWanderPoint(inst)
- Description: If a nearby player is present (via
GetNearestPlayer(true)), returns the player's position as a wander target; otherwise,nil. - Parameters:
inst(Entity instance). - Returns: World position vector (
Vector3) ornil.
isfoodnearby(inst)
- Description: Searches for edible food within
SEE_DIST(30 units) that is floating on water (no platform and not on ground). If a valid food item is found and not already assigned, registers cleanup callbacks foronremoveandonpickupevents, setsinst.foodtoeat, and returns aBufferedActionfor eating. Avoids targeting food that is already too close (< 6 units). - Parameters:
inst(Entity instance). - Returns:
BufferedActionornil. - Error states: Returns
nilif no food is found, if food is too close, or if it’s currently being held (checked viaFindEntityand distance filtering).
EatFishAction(inst)
- Description: Attempts to locate and target an ocean fish (
"oceanfish"tag) in the immediate area if the shark is not already eating. Only initiates the action if no"gobble_cooldown"timer is active. Checks viaFindEntitythat the shark is in ocean terrain. - Parameters:
inst(Entity instance). - Returns:
BufferedActionornil. - Error states: Returns
nilif a cooldown timer exists ("gobble_cooldown"), if no fish is found, or if the targeted fish is being held (validated viaIsHeld()frominventoryitemcomponent).
GetBoatFollowPosition(inst)
- Description: Finds and follows the nearest boat within 20 units, using a two-step process: First checks if
inst.targetboatis set, or attempts to find one and sets a"targetboatdelay"timer (10 seconds). Once a boat is identified, computes the follow offset based on its velocity (if moving fast enough) usingGetFormationOffsetNormal, or otherwise positions the shark behind the boat at a fixed distance (BOAT_TARGET_DISTANCE). This function is currently commented out in the behavior tree (theLeashbehavior line is commented). - Parameters:
inst(Entity instance). - Returns: Target position vector (
Vector3) ornil(if no boat is found). - Error states: If
targetboatbecomes invalid during follow, may continue to referencenil. Relies onBOAT_TARGET_DISTANCEbeing defined elsewhere.
GetBoatFollowDistance(inst)
- Description: Returns the acceptable follow distance threshold to the boat. Returns
0.5if the boat is moving faster than the shark's walk speed (SHARK_WALK_SQ); otherwise returnsMAX_BOAT_FOLLOW_DIST. - Parameters:
inst(Entity instance). - Returns:
number(distance threshold). - Error states:
MAX_BOAT_FOLLOW_DISTmust be defined elsewhere.
ShouldLeashRun(inst)
- Description: Returns
trueif the shark should run toward (or maintain position relative to) a boat when it is moving above the shark's walk speed. - Parameters:
inst(Entity instance). - Returns:
trueif boat velocity squared >=SHARK_WALK_SQ; otherwisefalse.
GetRunAwayTarget(inst)
- Description: Returns the current combat target (e.g., the player being chased) only if the
"getdistance"timer exists—likely used for flee behavior during close encounters. - Parameters:
inst(Entity instance). - Returns:
inst.components.combat.targetornil.
removefood(inst, target)
- Description: Utility to clean up event listeners and clear
inst.foodtoeatandinst._removefoodwhen a targeted food entity is removed or picked up. - Parameters:
inst(Entity instance),target(Entity instance). - Returns: None.
- Error states: Safe only when
inst._removefoodis non-nil.
Events & listeners
- Listens to:
"onremove"and"onpickup"events from food entities (viainst:ListenForEvent(...)inisfoodnearby) to trigger cleanup of the food reference. - Pushes:
"dobite"event viainst:PushEvent("dobite")inside theAttackhelper function (called viaDoAction(self.inst, Attack, "attack", true)).