Skip to main content

Last Update: 2023-07-06

Eater Component

API Version: 619045

The Eater component allows entities to consume edible items. It manages diet restrictions, eating behaviors, and handles the effects of consumed food.

Basic Usage

-- Add an eater component to an entity
local entity = CreateEntity()
entity:AddComponent("eater")

-- Configure the eater component
local eater = entity.components.eater
eater:SetDiet({FOODTYPE.VEGGIE, FOODTYPE.MEAT}, {FOODTYPE.ELEMENTAL, FOODTYPE.GEARS})
eater:SetCanEatHorrible(true)

Properties

PropertyTypeDescription
preferseatingTableList of food types the entity prefers to eat
caneatTableList of food types the entity can eat
cannoteatTableList of food types the entity cannot eat
caneathealthvalueBooleanWhether entity benefits from food health value
caneathungervalueBooleanWhether entity benefits from food hunger value
caneatsanityvalueBooleanWhether entity benefits from food sanity value
caneatraweBooleanWhether entity can eat raw meat
strongstomachBooleanWhether entity is immune to monster food penalties
abletoeatBooleanWhether entity can currently eat
eatsizecallbackFunctionCalled to determine how much of a stack to eat

Key Methods

Diet Configuration

-- Set which food types can be eaten
eater:SetDiet({FOODTYPE.MEAT}, {FOODTYPE.VEGGIE}) -- Can eat meat, cannot eat veggies

-- Set which food values are applied
eater:SetAbsorptionModifiers(1, 1, 0.5) -- Full health/hunger, half sanity

-- Configure special eating abilities
eater:SetCanEatHorrible(true) -- Can eat monster meat without penalties
eater:SetCanEatRaw(false) -- Cannot eat raw meat
eater:SetStrongStomach(true) -- Immune to food spoilage penalties

Eating Actions

-- Try to eat a specific food
local did_eat = eater:Eat(food_item)

-- Check if can eat a specific food
local can_eat = eater:CanEat(food_item)

-- Calculate food values when eaten by this eater
local health, hunger, sanity = eater:GetEdibleValue(food_item)

-- Set callback for when food is eaten
eater:SetOnEatFn(function(inst, food)
-- Do something when food is eaten
print(inst.prefab .. " ate " .. food.prefab)
end)

Related functions: When an entity eats food with the Eat() function, several component interactions occur. First, values are retrieved from the Edible Component using its GetHealth(), GetHunger(), and GetSanity() methods. Then, these values are applied to the entity's Health Component, Hunger Component, and Sanity Component using their respective DoDelta() methods. For temperature effects, the Temperature Component is accessed via SetTemperatureInBelly().

Diet Types

Eaters can have various diet restrictions:

  • Omnivore - Can eat everything (default for players)
  • Carnivore - Can only eat meat
  • Vegetarian - Can only eat vegetables/fruits
  • Specialized - Custom diet restrictions (like only eating wood, souls, etc.)

Integration with Other Components

The Eater component often works with:

  • Edible - For items that can be eaten
  • Health - To apply health effects from food
  • Hunger - To apply hunger effects from food
  • Sanity - To apply sanity effects from food
  • Temperature - For temperature effects from food
  • Inventory - For managing eaten items

See also

Example: Creating a Specialized Eater

local function MakeWoodEater()
local inst = CreateEntity()

-- Add basic components
inst.entity:AddTransform()
inst.entity:AddAnimState()

-- Add health and hunger
inst:AddComponent("health")
inst.components.health:SetMaxHealth(150)

inst:AddComponent("hunger")
inst.components.hunger:SetMax(100)

-- Configure specialized eater that only eats wood
inst:AddComponent("eater")
local eater = inst.components.eater

-- Can only eat items with FOODTYPE.WOOD
eater:SetDiet({FOODTYPE.WOOD}, {FOODTYPE.MEAT, FOODTYPE.VEGGIE, FOODTYPE.GENERIC})

-- Apply only hunger value, no health/sanity
eater:SetAbsorptionModifiers(0, 1, 0)

-- Add special effect when eating wood
eater:SetOnEatFn(function(inst, food)
-- Gain armor temporarily when eating wood
if inst.components.armor == nil then
inst:AddComponent("armor")
end
inst.components.armor:SetPercent(1)
inst.components.armor:SetAbsorption(0.5)

-- Remove armor after 60 seconds
inst:DoTaskInTime(60, function()
inst:RemoveComponent("armor")
end)
end)

return inst
end