Inventory system - thaalesalves/ai-games-research GitHub Wiki
Welcome to the inventory system's wiki page. Here you can find more info on how the script works.
This framework is, as the name suggests, used for keeping track of the player's inventory. It uses a bunch of mechanics to achieve this goal, but you can always customize it to your needs. It saves items into an array of objects with the info on each item, including the current status (whether it's being worn or not), and also modifies the player's WI so the AI always knows what's in the player's inventory and what items are currently equipped. The player WI is defined using Zaltys' format.
Feel free to fork my repo and modify it to your needs. You can change the WI format, integrate it with other scripts (it's been tested with my character parse scripts, Gnurro's placeholders and Gnurro's RPGmech, and they work together very well with a bit of modification to the scripts). If you have ideas or manage to optimize the code and implement new features, please let me know. You may create a pull request with new features or fixes, and I'll gladly read them and accept them if they're good. Also, if you need help, ping me at AIDCord or open an issue here at GitHub.
This is a list of commands that are available in the script. Note that the commands are case sensitive, so type them as you see them here. Item name is not case sensitive, though. It will always save items in your inventory in lower case.
-
/invCheck
: checks what's inside your inventory. Also outputs what's equipped at the moment. -
/invAdd <item name> <number>
: if empty,number
defaults to 1. Adds a new item to your inventory. -
/invRemove <item name> <number>
: if empty,number
defaults to 1. Removes an item from your inventory. -
/invEquip <item name>
: equips an item if you have at least one instance of it in your inventory. -
/invDebug
: reloads player WI and fixes it with the current inventory and equipped items. Use this command if you run into issues with the player WI not being found (orundefined
). -
/scenarioHelp
: prints the list of available commands. -
/scenarioHelp <command>
: prints usage and instructions to the supplied command. -
/invMechanics <enable or disable>
: enables or disables inventory mechanics
Adding an iron sword to the inventory
/invAdd iron sword 1
> You add 1 iron sword to your inventory.
You have added 1 iron sword to your inventory.
Removing the only iron sword in the inventory
/invRemove iron sword 1
> You remove 1 iron sword from your inventory.
You have removed all iron sword from your inventory.
If there are more than 1 of the item in the inventory
/invRemove iron sword 1
> You remove 1 iron sword from your inventory.
You have removed 1 iron sword from your inventory.
Checking inventory
/invCheck
> You check your inventory.
Your inventory contains: wooden bow (1x), leather tights (1x). Items equipped: wooden bow, leather tights.
Debugging inventory
/invDebugWi
> Your inventory and player WI have been debugged. New player WI saved at index 52
Specific command reference
/scenarioHelp
Example: /invAdd <object name> <quantity>
Adds objects to the player's inventory
Toggle inventory mechanics
/invMechanics
Example: /invMechanics enable
Toggles inventory mechanics
-
LETTER_REGEX
: used to parse digits from letters. This regex detects and selects digits so they're removed from a string. Identifies an item's name. -
DIGIT_REGEX
: used to parse letters from digits. This regex detects and selects letters so they're removed from a string. Identifies an item's quantity. -
PUNCTUATION_REMOVE
: as the name suggests, it detects and selects punctuation to "purify" an item's name. -
WEAPONS
: an array of string containing keywords that the framework will detect as weapons so they're equippable in the weapon slot. -
CLOTHING
: an array of string containing keywords that the framework will detect as clothing/armor so they're equippable in the clothing/armor slot.
-
capitalize(itemName: String)
: capitalizes the first letter in a given string. Used for formatting item names when they're returned to the player. -
findItemInInventory(itemName: String)
: searches for an item in inventory, and returns it if found. -
addToInventory(itemName: String, itemQuantity: Integer)
: adds given number of a given item to inventory. Modifies INV entry of player's WI. -
removeFromInventory(itemName: String, itemQuantity: Integer)
: removes given number of a given item from inventory. Modifies INV entry of player's WI. -
checkInventory()
: checks what's inside the inventory, and returns the items found. Returns equipped items as well. -
getInventory()
: returns inventory array. Also checks if it's defined. If it's not, initializes the inventory with an empty array. -
equipItem(itemName: String)
: equips given item. If the item is not equippable or the player doesn't have the required item in inventory, returns a message. Modifies WORN entry of player's WI. -
debugInventory()
: if player's WI index is lost, this function finds it again and modifies it according to the currently owned and equipped items. -
updateInventory()
: function called byremoveFromInventory()
andaddToInventory()
to modify the INV entry of player's WI. -
getType(itemName: String)
: checks item name againstWEAPON
andCLOTHING
arrays to see if the given item is equippable.
I added console.log()
to every function start and end, so you may track what's happening exactly. If you run into issues, open up the scripting screen from AID's UI and debug the script using the logs. I reiterate that if you can't manage to make it work, you can ping me on Discord or just open a new issue here. Log example:
"START addToInventory(): adding 1 instances of \"Rusty Sword\" to player's inventory."
"START findItemInInventory(): Looking for item \"rusty sword\" in player's inventory."
"START getInventory(): verifying player's inventory."
"END getInventory(): player's inventory is not empty. Returning it's contents."
"Player has no other instances of this item in their inventory. Adding these."
"END addToInventory(): 1 instances of \"Rusty Sword\" added to player's inventory."