Foodmemory
Overview
This component maintains a time-limited memory of consumed food items for an entity (typically a player), recording how many times each food has been eaten and fading memories over time. It supports food grouping (via spicedfoods base-name mapping) and enables external systems to retrieve current memory counts or applicable multipliers based on repeated consumption.
Dependencies & Tags
- Relies on:
spicedfoodsmodule for base food name resolution. - Entity must have:
inst:DoTaskInTime()(i.e., the entity should be aGOOPY/Simobject with task scheduling). - No tags are added or removed by this component.
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
inst | Entity | nil (assigned in constructor) | Reference to the entity this component belongs to. |
duration | number | TUNING.TOTAL_DAY_TIME | Time in seconds before a food memory expires and is forgotten. |
foods | table | {} | Table mapping base food prefab names to memory records (each record includes count and task). |
mults | table? | nil | Optional ordered list of multipliers; index corresponds to memory count. |
Main Functions
OnRemoveFromEntity()
- Description: Called when the component is removed from its entity. Cancels all pending food-expiration tasks to prevent memory leaks.
- Parameters: None.
SetDuration(duration)
- Description: Updates the duration (in seconds) after which a food memory expires.
- Parameters:
duration(number): New expiration time in seconds.
SetMultipliers(mults)
- Description: Sets the list of multipliers to apply based on the number of times a food has been remembered.
- Parameters:
mults(table): An array of numbers where indexicorresponds to multiplier foriinstances of the food in memory.
GetBaseFood(prefab)
- Description: Resolves the base food name for a given prefab. Spiced or variant foods map back to their canonical base name using the
spicedfoodsmapping. - Parameters:
prefab(string): The prefab name of the food.
RememberFood(prefab)
- Description: Registers consumption of a food item. If already present, increments count and resets the forgetting timer; otherwise, creates a new memory entry with initial count 1 and schedules expiration.
- Parameters:
prefab(string): The prefab name of the food consumed.
GetMemoryCount(prefab)
- Description: Returns how many times the given food has been remembered (i.e., consumed) and not yet forgotten.
- Parameters:
prefab(string): The prefab name of the food.
GetFoodMultiplier(prefab)
- Description: Returns the multiplier associated with the current memory count for the food. If no memory exists or
multsis not set, returns1. - Parameters:
prefab(string): The prefab name of the food.
- Notes: Uses
math.min(#mults, count)to clamp index within bounds.
OnSave()
- Description: Serializes active food memories into a saveable structure, storing count and remaining time until expiration for each.
- Returns:
table?— A table with keyfoods, mapping base food names to{ count = number, t = number }. Returnsnilif no memories exist.
OnLoad(data)
- Description: Restores food memories from saved data, rescheduling expiration tasks based on stored remaining times.
- Parameters:
data(table): Save data containingdata.foods, where keys are base food names and values are{ count, t }.
Events & Listeners
- Listens to:
- Internal expiration via
self.inst:DoTaskInTime(...), which invokesOnForgetFoodwhen the scheduled time elapses. OnForgetFood(inst, self, prefab)— Internal callback that removes a food entry from memory (self.foods[prefab] = nil).
- Internal expiration via
- Does not push or listen to any broadcast game events (e.g.,
"onupdate","onkilled").