Inventory API
The Beep8 Inventory API provides methods to manage and interact with the player’s inventory in a game. It allows you to add, remove, check, and filter items in the inventory.
beep8.Inventory.add(id [,amount])
Add a certain amount of an item to the inventory.
If a max is provided, the item count will not exceed that max.
Parameters
- id (string): The item ID to add.
- [amount] (number): The amount to add (default: 1). (default: 1)
beep8.Inventory.filter(match)
Filters inventory items based on a prefix or a regular expression.
This function returns an array of objects, each containing the id
and count
of items that match the given prefix or regular expression.
Examples:
-
To get all keys by prefix: const keys = beep8.Inventory.filter(“key-“);
-
To use a regex for finer control: const special = beep8.Inventory.filter(/^potion-|^scroll-/);
Parameters
- match (string|RegExp): Item ID prefix or regex to match.
Returns
- (Array<{id:string,count:number): >} Matching items with counts.
beep8.Inventory.getCount(id)
Get the count of a specific item in the inventory.
Parameters
- id (string): The item ID to check.
Returns
- (number): The count of the item (default: 0 if not found).
beep8.Inventory.getFlag(flag)
Get a flag from the inventory.
Returns true/false if the flag is set.
Parameters
- flag (string): The flag name to get.
Returns
- (boolean): The value of the flag (default: false).
beep8.Inventory.has(id [,amount])
Check if the inventory has at least a certain amount of an item.
Parameters
- id (string): The item ID to check.
- [amount] (number): The minimum amount required (default: 1). (default: 1)
Returns
- (boolean): True if the inventory has at least the specified amount, false otherwise.
beep8.Inventory.remove(id)
Remove a certain amount of an item from the inventory.
If the item count goes below zero, it is set to zero.
Parameters
- id (string): The item ID to remove.
beep8.Inventory.reset()
Reset the inventory to empty.
This clears all item counts and flags.
beep8.Inventory.setFlag(flag)
Set a flag in the inventory.
For things like “door opened”, “cutscene played”
Parameters
- flag (string): The flag name to set.