Inventory
Read more about the Inventory system in Hiro here.
Functions #
List #
List the items defined as well as the computed item sets for the user by ID.
| |
ListInventoryItems #
List the items which are part of a user’s inventory by ID.
| |
ConsumeItems #
Deduct the item(s) from the user’s inventory and run the consume reward for each one, if defined.
Items can be consumed by using their:
- Item ID, so that any instance of the item will be consumed.
- Instance ID, so that a specific instance of the item will be consumed.
For example, if the player has 2 potions in the inventory, one with instance ID x and one with yif they consume items potion: 1 either x or y might be consumed,
if they consume x: 1 and that will specifically consume x and not touch y.
You can put nil on which parameter you don’t wanna use.
| |
GrantItems #
Add the item(s) to a user’s inventory by ID.
| |
UpdateItems #
Update the properties which are stored on each item by instance ID for a user.
This function uses conditional writes to ensure data consistency. When updating items, it will use the current inventory version if it exists (after retrieving it), otherwise it will use * to indicate any version is acceptable.
| |
Hooks #
SetOnConsumeReward #
Set a custom reward function which will run after an inventory item’s consume reward is rolled.
| |
SetConfigSource #
Sets a custom callback function that is called when an item is not found in the static configuration. This is useful for dynamic item catalogs where items are generated procedurally, stored in a database, or fetched from an external service.
The function receives the itemID and should return the item’s configuration if found.
| |
Returns:
(*hiro.InventoryConfigItem, nil)when the item is found(nil, nil)when the item doesn’t exist(nil, error)for errors (database unavailable, etc.)
ConfigSource is a fallback for looking up individual items not in the static config. It’s set on a specific system and returns a single item configuration.
Personalizer replaces the entire system config for a user. See Personalizer for more information.
These can work together: Personalizer runs first and may replace the entire config, then ConfigSource serves as a fallback for items not in that (potentially personalized) config.
Property merging behavior #
When retrieving inventory items, property values are merged between the item instance and its config definition. The merge follows these rules:
- Only properties that don’t already exist in the item instance are added from the config source.
- Properties that exist in both the item instance and config source will retain the item instance value.
For example:
Item instance in user’s inventory:
| |
ConfigSource returns:
| |
Final merged result:
| |
In this example, rarity keeps the item instance value ("legendary") rather than the config value ("common"), while damage_type and durability are added from the config since they don’t exist in the item instance.
This behavior allows you to set default values in your config source while preserving any customizations made to individual item instances.
