Recipestockpile
Overview
The RecipeStockpile component tracks and manages per-recipe inventory stock levels for an entity (typically a workbench or storage structure), including automatic restocking via timers, state-aware callbacks (e.g., when restocked or emptied), and save/load synchronization. It enables gameplay mechanics where recipe production depletes a limited stock that regenerates over time.
Dependencies & Tags
- Component Dependency: Relies on
inst:DoTaskInTime(...)andGetTaskRemaining(...)— indicating integration with the game's task scheduler (TheSimorGlobalTaskManager). - Event Listener: Registers for the
"builditem"event on its owner entity. - Tags: Adds no entity tags.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
stock | table | {} | A dictionary mapping recipe names to stock metadata (see SetupItem). Each entry holds num, max, restocktime, onrestockfn, onemptyfn, and optionally timer. |
Main Functions
SetupItem(data, start_restock_timer)
- Description: Initializes or updates stock configuration for a given recipe. Sets initial quantity (
num), maximum capacity (max), restock interval (restocktime), and optional callbacks. If enabled, starts a restock timer if stock is below max. - Parameters:
data(table): Table with keys:recipe(string),num(number, optional),max(number),restocktime(number, optional),onrestockfn(function, optional),onemptyfn(function, optional).numdefaults tomaxif missing.start_restock_timer(boolean): Whether to start a restock timer ifnum < maxandrestocktimeis defined.
OnItemCrafted(recipe)
- Description: Decrements stock for the given recipe upon a crafting event. Triggers restock timer if applicable, and fires
onemptyfnwhen stock reaches 0. - Parameters:
recipe(string): Name of the recipe whose stock is being consumed.
RemoveStock(recipe, allow_restock)
- Description: Sets the current stock of a specific recipe to 0, cancels any running restock timer, and (if enabled) may restart a restock timer. Fires
onemptyfnafter clearing. - Parameters:
recipe(string): Recipe name to clear.allow_restock(boolean): If true, restarts restock timer based onrestocktime(note: appears to contain a typo — usestime.restocktimeinstead ofstock.restocktimein current code).
RemoveAllStock(allow_restock)
- Description: Clears stock for all recipes, cancels their timers, and optionally restarts restock timers. Fires
onemptyfnfor each recipe. - Parameters:
allow_restock(boolean): If true, restarts restock timers for all cleared recipes.
FullyRestockItem(recipe)
- Description: Immediately fills the stock of a recipe to its maximum, cancels any active timer, and fires
onrestockfn. - Parameters:
recipe(string): Recipe name to fully restock.
HasAnyStock()
- Description: Checks if any recipe in the stockpile currently has
num > 0. - Returns:
boolean—trueif at least one recipe has remaining stock; otherwisefalse.
OnSave()
- Description: serializes current stock state for saving to disk. Captures
numand remaining time on the timer (if any). - Returns:
table— Map of recipe names to{ num = number, timer = number? }.
OnLoad(data)
- Description: Restores stock state after loading. Uses saved
numandtimervalues to reinitialize timers. - Parameters:
data(table): Deserialized stock data (keyed by recipe name).
GetDebugString()
- Description: Generates a human-readable debug string listing each recipe’s current/max stock and remaining restock time (if any).
- Returns:
string— Multi-line string for in-game debug console.
Events & Listeners
- Listens to
"builditem"event — Triggersonitemcrafted, which forwards the recipe name toOnItemCrafted. - Cleans up listener on removal via
OnRemoveFromEntity.
Notes & Known Issues
- In
RemoveStockandRemoveAllStock, the expressiontime.restocktimeis likely a typo; should bestock.restocktime. This may cause incorrect or failed restock restarts ifallow_restockistrue.