Edible
Based on game build 722832 | Last updated: 2026-04-28
Overview
Edible defines the nutritional and status effects of food items when consumed by entities with an eater component. It calculates health, hunger, and sanity values with modifiers for spoilage state (stale/spoiled), spice additives, and food affinity bonuses. The component also supports temperature effects (heating/cooling) and chill percentage for cold foods. Property changes to healthvalue, sanityvalue, foodtype, or secondaryfoodtype automatically trigger watcher callbacks that update entity tags.
Usage example
local inst = CreateEntity()
inst:AddComponent("edible")
inst.components.edible.healthvalue = 20
inst.components.edible.hungervalue = 15
inst.components.edible.foodtype = FOODTYPE.MEAT
inst.components.edible:SetOnEatenFn(function(food, eater)
print("Food was eaten by", eater.prefab)
end)
Dependencies & tags
External dependencies:
TUNING-- provides default values for stale/spoiled food multipliers and spice effects
Components used:
perishable-- checked forIsStale()andIsSpoiled()to apply degradation multiplierseater-- accessed on the consuming entity forignoresspoilage,stale_hunger,spoiled_health, etc.foodaffinity-- accessed on the consuming entity viaGetAffinity()for hunger multiplier bonusstackable-- used for stack operations inHandleEatRemove()andGetStackMultiplier()temperature-- accessed on the consuming entity viaSetTemperatureInBelly()for temperature effects
Tags:
badfood-- added whenhealthvalueorsanityvalueis negative; removed on cleanupedible_<foodtype>-- added based onfoodtypeandsecondaryfoodtype(e.g.,edible_generic,edible_meat)spoiledfood-- added/removed bySetForceSpoiledFood()
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
healthvalue | number | 10 | Base health restoration. Assignment triggers oncheckbadfood watcher. |
hungervalue | number | 10 | Base hunger restoration. |
sanityvalue | number | 0 | Base sanity restoration. Assignment triggers oncheckbadfood watcher. |
foodtype | string | FOODTYPE.GENERIC | Primary food type for affinity matching. Assignment triggers onfoodtype watcher. |
secondaryfoodtype | string | nil | Secondary food type for affinity matching. Assignment triggers onfoodtype watcher. |
oneaten | function | nil | Callback fired when food is eaten. Signature: fn(inst, eater). Set via SetOnEatenFn(). |
degrades_with_spoilage | boolean | true | If false, spoilage state does not reduce food values. |
gethealthfn | function | nil | Override function for health calculation. Signature: fn(inst, eater) → number. |
getsanityfn | function | nil | Override function for sanity calculation. Signature: fn(inst, eater) → number. |
temperaturedelta | number | 0 | Temperature change applied to eater (positive = heat, negative = cool). |
temperatureduration | number | 0 | Duration of temperature effect in seconds. |
chill | number | 0 | Percentage [0, 1] of temperatureduration for chill effect. Only applies if temperaturedelta > 0. |
stale_hunger | number | TUNING.STALE_FOOD_HUNGER | Multiplier applied to hunger when food is stale. |
stale_health | number | TUNING.STALE_FOOD_HEALTH | Multiplier applied to health when food is stale. |
spoiled_hunger | number | TUNING.SPOILED_FOOD_HUNGER | Multiplier applied to hunger when food is spoiled. |
spoiled_health | number | TUNING.SPOILED_FOOD_HEALTH | Multiplier applied to health when food is spoiled. |
spice | string | nil | Spice type identifier. Applies multipliers from TUNING.SPICE_MULTIPLIERS. |
overridestackmultiplierfn | function | nil | Override for GetStackMultiplier(). Signature: fn(inst) → number. |
handleremovefn | function | nil | Override for HandleEatRemove(). Signature: fn(inst, eatwholestack). |
spoiledfood | boolean | false | Internal flag set by SetForceSpoiledFood(). |
Main functions
oncheckbadfood(self) (local)
- Description: Property watcher callback for
healthvalueandsanityvalue. Adds or removes thebadfoodtag based on whether either value is negative. - Parameters:
self-- the Edible component instance. - Returns: None
- Error states: None
onfoodtype(self, new_foodtype, old_foodtype) (local)
- Description: Property watcher callback for
foodtypeandsecondaryfoodtype. Removes the oldedible_<old_foodtype>tag and adds the newedible_<new_foodtype>tag. Asserts that main and secondary food types are not identical. - Parameters:
self-- the Edible component instancenew_foodtype-- the new food type stringold_foodtype-- the previous food type string (may benil)
- Returns: None
- Error states: Asserts if
self.foodtype == self.secondaryfoodtype— main and secondary types cannot match.
OnRemoveFromEntity()
- Description: Cleanup handler called when component is removed. Removes
badfood,edible_<foodtype>,edible_<secondaryfoodtype>, andedible_berrytags from the entity. - Parameters: None
- Returns: None
- Error states: None
GetWoodiness(eater)
- Description: Deprecated function. Always returns
0. - Parameters:
eater-- entity instance (unused) - Returns:
0 - Error states: None
GetSanity(eater)
- Description: Calculates the sanity value this food provides. Applies spoilage degradation (stale returns
0, spoiled returns-TUNING.SANITY_SMALL), spice multipliers, and respectsignoresspoilageflag. Ifgetsanityfnis set, uses that instead ofsanityvalue. - Parameters:
eater-- entity consuming the food (may benil) - Returns: Number sanity value (may be negative for spoiled food)
- Error states: None
GetHunger(eater)
- Description: Calculates the hunger value this food provides. Applies spoilage multipliers (
stale_hunger,spoiled_hunger), food affinity bonus fromeater.components.foodaffinity:GetAffinity(), and respectsignoresspoilageflag. - Parameters:
eater-- entity consuming the food (may benil) - Returns: Number hunger value after all multipliers
- Error states: None
GetHealth(eater)
- Description: Calculates the health value this food provides. Applies spoilage multipliers (
stale_health,spoiled_health), spice health multipliers, and respectsignoresspoilageflag. Spoiled food disables spice effects. Ifgethealthfnis set, uses that instead ofhealthvalue. - Parameters:
eater-- entity consuming the food (may benil) - Returns: Number health value after all multipliers
- Error states: None
GetDebugString()
- Description: Returns a formatted debug string showing food type and base stat values.
- Parameters: None
- Returns: String in format
"Food type: <type>, health: <h>, hunger: <hu>, sanity: <s>" - Error states: None
SetOnEatenFn(fn)
- Description: Sets the callback function to fire when this food is eaten.
- Parameters:
fn-- function with signaturefn(inst, eater) - Returns: None
- Error states: None
SetHandleRemoveFn(fn)
- Description: Sets the callback function to handle food removal after eating.
- Parameters:
fn-- function with signaturefn(inst, eatwholestack) - Returns: None
- Error states: None
SetOverrideStackMultiplierFn(fn)
- Description: Sets the override function for calculating stack multiplier.
- Parameters:
fn-- function with signaturefn(inst) → number - Returns: None
- Error states: None
SetForceSpoiledFood(spoiled)
- Description: Forces the food into a spoiled state regardless of actual perishable state. Adds or removes the
spoiledfoodtag and sets internalspoiledfoodflag. - Parameters:
spoiled-- boolean to force spoiled state - Returns: None
- Error states: None
IsSpoiledFood()
- Description: Returns whether the food has been forced into spoiled state via
SetForceSpoiledFood(). - Parameters: None
- Returns: Boolean
trueif forced spoiled,falseotherwise - Error states: None
SetGetHealthFn(fn)
- Description: Sets the override function for health calculation.
- Parameters:
fn-- function with signaturefn(inst, eater) → number - Returns: None
- Error states: None
SetGetSanityFn(fn)
- Description: Sets the override function for sanity calculation.
- Parameters:
fn-- function with signaturefn(inst, eater) → number - Returns: None
- Error states: None
OnEaten(eater)
- Description: Called when the food is consumed. Fires the
oneatencallback, applies temperature effects to the eater's belly temperature (iftemperaturedeltaandtemperaturedurationare set), plays eat sound, and pushes theoneatenevent. - Parameters:
eater-- entity consuming the food - Returns: None
- Error states: None
HandleEatRemove(eatwholestack)
- Description: Removes the food item after eating. Calls
handleremovefnif set, otherwise removes one item from stack ifeatwholestackisfalseandstackableexists, or removes the entire entity. - Parameters:
eatwholestack-- boolean whether to remove entire stack - Returns: None
- Error states: None
GetStackMultiplier()
- Description: Returns the multiplier for stacking effects. Uses
overridestackmultiplierfnif set, otherwise returns stack size fromstackablecomponent, or1as fallback. - Parameters: None
- Returns: Number multiplier (typically stack size)
- Error states: None
AddChill(delta)
- Description: Increases the chill percentage by
delta. Only applies iftemperaturedelta > 0andnochillis not set. Clamps result to[0, 1]. - Parameters:
delta-- number to add to chill percentage - Returns: None
- Error states: None
DiluteChill(item, count)
- Description: Averages chill percentage when stacking with another edible item. Formula:
(stacksize * self.chill + count * item.chill) / (stacksize + count). Only applies iftemperaturedelta > 0,nochillis not set, and both items have required components. - Parameters:
item-- the other edible item being stackedcount-- number of items being added to stack
- Returns: None
- Error states: None
OnSave()
- Description: Returns save data table containing
chillvalue if greater than0. - Parameters: None
- Returns: Table
{ chill = number }ornilif chill is0 - Error states: None
OnLoad(data)
- Description: Restores
chillvalue from save data. Clamps to[0, 1]. Only applies iftemperaturedelta > 0andnochillis not set. - Parameters:
data-- save data table fromOnSave() - Returns: None
- Error states: None
Events & listeners
- Pushes:
oneaten— fired inOnEaten()when food is consumed. Data:{ eater = entity }