Itemstore
Overview
The ItemStore component stores item records in memory for later reconstruction, typically used by containers or systems that need to preserve item state across saves or sessions. It does not hold live entities directly but keeps serialized item_record data; items are spawned on demand (e.g., via GetFirstItems) and removed from the store. It integrates with the game’s save/load system via OnSave and OnLoad.
Dependencies & Tags
- Depends on
SpawnPrefab(a global utility for creating prefabs with optional skins and creators). - Does not add or remove entity tags.
- Does not require other components to function, though it is typically attached to an entity (e.g., a storage container) via
inst:AddComponent("itemstore").
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
storeditemdatas | table | {} | A list of item record entries; each entry is a table with item_record (required) and optionally migrationdata. |
Main Functions
GetNumberOfItems()
- Description: Returns the total number of item records currently stored.
- Parameters: None.
GetFirstItems(count)
- Description: Retrieves and spawns the first
countstored items as live entities, then removes them from the internal list. Spawns each item using its saved record and persistence data. Emits theitemstore_changedcountevent after modification. - Parameters:
count(number): The number of items to retrieve. If fewer thancountitems exist, returns as many as are available.
AddItem(item)
- Description: Adds a live item to the store by extracting its save record, removing the item from the world, and storing the record. Emits the
itemstore_changedcountevent. - Parameters:
item(Entity): A live item entity to be stored (it will be removed from the world after capture).
AddItemRecordAndMigrationData(item_record, migrationdata)
- Description: Adds a raw item record (and optional migration metadata) directly to the store, without requiring a live item. Useful for deserialization or importing items. Emits the
itemstore_changedcountevent. - Parameters:
item_record(table): A table containing prefab name, optional skin name, skin ID, and persist data—typically the output ofitem:GetSaveRecord().migrationdata(tableornil): Optional data used to track the origin session (e.g., for creator attribution), typically containing asessionid.
OnSave()
- Description: Returns the serializable state of the component for saving to disk. Returns
nilif the store is empty. - Parameters: None.
- Returns: A table of the form
{ itemdatas = self.storeditemdatas }, ornil.
OnLoad(data)
- Description: Restores the component state from saved data. Triggers the
itemstore_changedcountevent if items were loaded (i.e., if the count is non-zero). - Parameters:
data(tableornil): The saved data for this component, typically fromOnSave(). Ifnil, no action is taken.
Events & Listeners
- Emits:
"itemstore_changedcount": Triggered whenever the number of stored items changes (e.g., afterAddItem,GetFirstItems, orOnLoadwith non-zero items).
- Does not listen for any events (no
ListenForEventcalls found).