Edible
Overview
The Edible component is responsible for managing all properties related to an item's edibility within the game. It defines how an item affects a character's hunger, health, and sanity when consumed, and interacts with the Perishable component to apply spoilage penalties. It also handles food types, temperature effects, and custom callbacks for eating behavior.
Dependencies & Tags
This component interacts with or relies on the following other components and applies specific tags to its inst (the entity it's attached to):
- Other Components:
Perishable: Accessed to determine an item's freshness status (stale, spoiled).Eater(on the consuming entity): Accessed to check for spoilage immunity or custom spoilage multipliers.FoodAffinity(on the consuming entity): Accessed to apply affinity bonuses to hunger.Stackable: Accessed for stack size when handling removal or calculating stack multipliers.Temperature(on the consuming entity): Accessed to apply temperature effects after consumption.SoundEmitter(on the consuming entity): Accessed to play eat sounds.
- Tags Added/Removed by Edible:
badfood: Added ifhealthvalueis negative orsanityvalueis negative. Removed otherwise. (Managed whenhealthvalueorsanityvalueproperties are set).edible_<FOODTYPE>: Added based onfoodtypeandsecondaryfoodtypeproperties. Removed when the property changes or the component is removed. Example:edible_MEAT,edible_VEGGIE.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | - | A reference to the entity this component is attached to. |
healthvalue | number | 10 | The amount of health restored or lost when the item is eaten. Setting this property triggers a check for the "badfood" tag. |
hungervalue | number | 10 | The amount of hunger restored when the item is eaten. |
sanityvalue | number | 0 | The amount of sanity restored or lost when the item is eaten. Setting this property triggers a check for the "badfood" tag. |
foodtype | string | FOODTYPE.GENERIC | The primary category of food this item belongs to (e.g., FOODTYPE.MEAT, FOODTYPE.VEGGIE). Setting this property adds an edible_<foodtype> tag and removes any old edible_<old_foodtype> tag. Asserted not to be the same as secondaryfoodtype. |
secondaryfoodtype | string or nil | nil | A secondary category of food this item belongs to, if any. Setting this property adds an edible_<secondaryfoodtype> tag and removes any old edible_<old_secondaryfoodtype> tag. Asserted not to be the same as foodtype. |
oneaten | function or nil | nil | A custom callback function to be executed when the item is eaten. Takes (inst, eater) as arguments. |
degrades_with_spoilage | boolean | true | If true, the item's nutritional values will be reduced by spoilage (stale/spoiled). |
gethealthfn | function or nil | nil | A custom function to override the default health calculation. Takes (inst, eater) as arguments. |
getsanityfn | function or nil | nil | A custom function to override the default sanity calculation. Takes (inst, eater) as arguments. |
temperaturedelta | number | 0 | The change in temperature (e.g., body temperature) applied to the eater. |
temperatureduration | number | 0 | The duration for which the temperaturedelta effect lasts. |
chill | number | 0 | A percentage [0, 1] indicating the degree of "chill" applied to the food, reducing its temperature effects. Only applicable if temperaturedelta > 0. |
stale_hunger | number | TUNING.STALE_FOOD_HUNGER | Multiplier for hunger value when the food is stale. |
stale_health | number | TUNING.STALE_FOOD_HEALTH | Multiplier for health value when the food is stale. |
spoiled_hunger | number | TUNING.SPOILED_FOOD_HUNGER | Multiplier for hunger value when the food is spoiled. |
spoiled_health | number | TUNING.SPOILED_FOOD_HEALTH | Multiplier for health value when the food is spoiled. |
spice | string or nil | nil | The type of spice applied to the food, allowing TUNING.SPICE_MULTIPLIERS to modify effects. |
Main Functions
OnRemoveFromEntity()
- Description: Called when the
Ediblecomponent is removed from its entity. It ensures that all associated tags (badfood,edible_<foodtype>,edible_<secondaryfoodtype>, andedible_BERRYfor some reason) are cleaned up. - Parameters: None.
GetSanity(eater)
- Description: Calculates the sanity value provided by the food, considering spoilage, custom
getsanityfn,eaterproperties, andspicemultipliers. If the food is stale, positive sanity is zeroed; if spoiled, a fixed negative sanity is applied. - Parameters:
eater: TheEntityattempting to eat the food. May benil.
GetHunger(eater)
- Description: Calculates the hunger value provided by the food, considering spoilage,
eaterproperties (likeignoresspoilage,stale_hunger,spoiled_hunger), andfoodaffinity. - Parameters:
eater: TheEntityattempting to eat the food. May benil.
GetHealth(eater)
- Description: Calculates the health value provided by the food, considering spoilage, custom
gethealthfn,eaterproperties, andspicemultipliers. - Parameters:
eater: TheEntityattempting to eat the food. May benil.
GetDebugString()
- Description: Returns a formatted string containing the food's type, health, hunger, and sanity values for debugging purposes.
- Parameters: None.
SetOnEatenFn(fn)
- Description: Sets a custom callback function to be executed when the item is eaten. This function will receive
(inst, eater)as arguments. - Parameters:
fn: Afunctionto set as theoneatencallback.
SetHandleRemoveFn(fn)
- Description: Sets a custom callback function to handle the item's removal from the world after being eaten. This function will receive
(inst, eatwholestack)as arguments. - Parameters:
fn: Afunctionto set as thehandleremovefncallback.
SetOverrideStackMultiplierFn(fn)
- Description: Sets a custom function to determine the stack multiplier for the food. This function should return a number representing the effective stack size (e.g., for calculating total hunger from a stack).
- Parameters:
fn: Afunctionto set as theoverridestackmultiplierfncallback.
SetGetHealthFn(fn)
- Description: Sets a custom function to dynamically calculate the health provided by the food. This function will receive
(inst, eater)as arguments. - Parameters:
fn: Afunctionto set as thegethealthfncallback.
SetGetSanityFn(fn)
- Description: Sets a custom function to dynamically calculate the sanity provided by the food. This function will receive
(inst, eater)as arguments. - Parameters:
fn: Afunctionto set as thegetsanityfncallback.
OnEaten(eater)
- Description: The primary method called when the food is consumed. It executes the
oneatencallback, applies temperature effects to theeater(iftemperaturedeltaandtemperaturedurationare set), plays an eat sound, and pushes an "oneaten" event. - Parameters:
eater: TheEntitythat consumed the food.
HandleEatRemove(eatwholestack)
- Description: Handles the removal of the edible item after being eaten. It prioritizes the
handleremovefnif set. Otherwise, ifeatwholestackisfalseand the item is stackable, it removes one from the stack. Ifeatwholestackistrueor the item is not stackable, it removes the entire item. This is an internal function primarily called byeater.lua. - Parameters:
eatwholestack:boolean,trueif the entire stack was consumed,falseotherwise.
GetStackMultiplier()
- Description: Returns a multiplier representing the effective "size" of the food for nutritional calculations. It prioritizes
overridestackmultiplierfnif set, otherwise uses thestackablecomponent'sStackSize(), defaulting to1. - Parameters: None.
AddChill(delta)
- Description: Increases the
chillproperty of the food, diluting its temperature effects. Only applies iftemperaturedeltais positive andnochill(an unexposed internal property) isfalse. - Parameters:
delta:number, the amount to add tochill, typically scaled bytemperatureduration.
DiluteChill(item, count)
- Description: Averages the
chillvalue with another edible item when items are combined (e.g., stacking). - Parameters:
item: The otherEntitywith anEdiblecomponent to dilutechillwith.count:number, the stack count of the other item.
OnSave()
- Description: Returns save data for the
chillproperty if it's greater than0. - Parameters: None.
OnLoad(data)
- Description: Loads the
chillproperty from saveddata. - Parameters:
data:table, the saved data containing thechillvalue.
Events & Listeners
- Pushes Event:
oneaten: Triggered on theinst(the edible item) when it is consumed. The event data is{ eater = eater }, whereeateris the entity that ate the food.