Perishable
Overview
This component implements the logic for food spoilage in Don't Starve Together, tracking remaining freshness time, applying environmental and situational decay modifiers (e.g., temperature, refrigeration, wetness, acid rain), updating freshness tags (fresh, stale, spoiled), and triggering spoilage behavior (including on-perish replacement logic). It integrates with the ECS Update loop via a periodic task.
Dependencies & Tags
- Component Dependencies: None explicitly added in
_ctor; however, the component assumes existence of:inst.components.edible(used conditionally for temperature regulation)inst.components.stackable(used inDilute,Perish, andOnLoad)inst.components.inventoryitem(for owner and container slot retrieval)inst.components.occupier(optional, to determine owner when item is held/occupied)
- Tags Managed:
fresh,stale,spoiled— removed and added dynamically based on spoilage percentage.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | — | Reference to the owning entity. |
perishfn | function | nil | Callback invoked when item spoils. |
frozenfiremult | boolean | false | If true, applies extra decay multiplier for frozen items near fire. |
perishtime | number | nil | Total base spoilage time (seconds). Initialized via constructor parameter or SetPerishTime. |
perishremainingtime | number | nil | Remaining spoilage time (seconds). Updated dynamically during decay. |
updatetask | Task | nil | Periodic update task managing decay; nil when paused/stopped. |
start_dt | number | nil | Delay before first decay tick in StartPerishing. |
localPerishMultiplyer | number | 1 | Custom decay multiplier for mod-specific logic. |
onperishreplacement | string | nil | Prefab name to spawn on spoilage. |
Main Functions
Update(inst, dt)
- Description: Core decay handler. Computes effective decay rate based on environment (temperature, rain), container (fridge, preserver), owner traits (spoiler, preserver), item state (wetness, frozen), and multipliers. Advances
perishremainingtime, triggersperishchangeevents, chill-hot foods, and callsPerish()when expired. - Parameters:
inst: The entity owning the component.dt: Time delta; may be overridden bystart_dtor cachedstart_dt.
IsFresh()
- Description: Returns
trueif the entity has thefreshtag. - Parameters: None.
IsStale()
- Description: Returns
trueif the entity has thestaletag. - Parameters: None.
IsSpoiled()
- Description: Returns
trueif the entity has thespoiledtag. - Parameters: None.
Dilute(number, timeleft)
- Description: Adjusts spoilage time for stacked items (e.g., combining partial items). Only functional if
stackablecomponent exists. Recalculatesperishremainingtimeas a weighted average and triggersperishchange. - Parameters:
number: Stack size of the added item.timeleft: Remaining spoilage time of the added item.
AddTime(time)
- Description: Adds time to
perishremainingtime, capping atperishtime. Triggersperishchangeevent if the displayed percent changes. - Parameters:
time: Seconds to add.
SetPerishTime(time)
- Description: Sets both
perishtimeandperishremainingtimetotime. Automatically restarts perishing if a task is already running. - Parameters:
time: Total spoilage time in seconds.
SetLocalMultiplier(newMult)
- Description: Sets
localPerishMultiplyer, allowing mods to globally scale spoilage rate for this item. - Parameters:
newMult: Multiplier (e.g.,0.5for half decay rate).
GetLocalMultiplier()
- Description: Returns current
localPerishMultiplyer. - Parameters: None.
SetNewMaxPerishTime(newtime)
- Description: Updates
perishtimewhile preserving current spoilage percentage by recalculatingperishremainingtime. - Parameters:
newtime: New total spoilage time in seconds.
SetOnPerishFn(fn)
- Description: Assigns the callback function to execute upon spoilage.
- Parameters:
fn: A function that accepts the entity (inst) as its sole argument.
GetPercent()
- Description: Returns spoilage percentage (
0.0–1.0), calculated asperishremainingtime / perishtime. Returns0if invalid. - Parameters: None.
SetPercent(percent)
- Description: Sets
perishremainingtimebased onperishtimeand a percentage. Clampspercentbetween0and1. Restarts perishing if active. - Parameters:
percent: Spoilage percentage (0.0–1.0).
ReducePercent(amount)
- Description: Decreases spoilage percentage by
amount. - Parameters:
amount: Value subtracted from current percent (e.g.,0.1for 10% faster spoilage).
GetDebugString()
- Description: Returns human-readable debug info (e.g.,
"Perishing 120.00s"or"perished"), includingfrozenfiremultstatus. - Parameters: None.
LongUpdate(dt)
- Description: Handles decay during long tick intervals (e.g., world sleep). Uses
GetTaskRemainingto preserve decay precision across tick gaps. - Parameters:
dt: Delta time.
StartPerishing()
- Description: Begins or restarts the spoilage decay task (
Update). Cancels any existing task first. - Parameters: None.
IsPerishing()
- Description: Returns
trueif perishing is active (i.e.,updatetaskis notnil). - Parameters: None.
Perish()
- Description: Handles spoilage logic: cancels decay task, runs
perishfn, pushes"perished"event, and optionally replaces the item withonperishreplacementprefab. Handles stack size and inventory slot preservation. - Parameters: None.
StopPerishing()
- Description: Cancels the decay task without spoiling the item (pauses spoilage).
- Parameters: None.
OnSave()
- Description: Returns serialization table with
pausedstatus andperishremainingtime. (Note: The function body currently has areturnstatement afterreturn {}, making it nonfunctional—likely a typo in the source.) - Parameters: None.
OnLoad(data)
- Description: Loads saved state (
data.time,data.paused) and restores perishing state accordingly. - Parameters:
data: Table containingtime(number) andpaused(boolean) fields.
Events & Listeners
- Pushes Events:
"forceperishchange"— When freshness tags change (fresh/stale/spoiled)."perishchange"— Whenperishremainingtimechanges enough to alter the displayed percentage (checked every ~1% viamath.floorcomparison)."perished"— When spoilage completes.
- No
inst:ListenForEventcalls are present.