Inventory Component
This is a script that can be attached to any node and it turns it into a component. This component can be a child of any scene (Player, Chest, etc...)
You can modify some of the properties of the Inventory Component right in the inspector!
Properties
String | inv_name | "Name" |
int | inv_slots | 5 |
Array[InvSlotStruct] | start_items | |
Array[InvSlotStruct] | inv_item_list | |
InvWindow | window_ref | null |
Array[InventoryComponent] | joined_invs | |
Object | interactor | null |
Methods
bool | add_to_inv ( BaseItem struct, int amount ) |
Array[bool, int] | inv_query ( String item_name, int item_amount ) |
bool | inv_remove_by_name ( String item_name, int item_amount ) |
void | use_item_at_slot ( int index ) |
void | refresh_slot_at_index ( int index ) |
void | swap_items_by_ref ( InvSlotStruct from_ref, InvSlotStruct to_ref ) |
Property Descriptions
String inv_name = "Name"
This is used for the name when creating an Inventory Window. Not required, can be left empty or unmodified.
int inv_slots = 5
How many slots an Inventory has.
Array[InvSlotStruct] start_items
You can add them through the inspector. These items will be added to the Inventory Component on the start of the game.
Array[InvSlotStruct] inv_item_list
This Array holds the data to all your items. Many nodes thoughout the system use this array to show the needed data.
InvWindow window_ref = null
When spawning an Inventory Window, you should set the window_ref
to the Window. When you destroy the Window, you should set this to null. Used in the refresh_slot_at_index
function, because the InvWindow
has access to every slot, and when using a slot we need to update it.
Array[InventoryComponent] joined_invs
You can join inventories together, example: hotbar with backpack. This way when you call function such as inv_query
or add_to_inv
, it will check the inventory component the function is called from, and all the joined_invs in it.
Object interactor = null
Every inventory should have an interactor while it is being used/opened. That way the items, when used can call functions directly on the interactor. Example: a chest with inventory component has no interactor until a player opens it. When a player opens the chest, the interactor of the inventory component on the chest should be set to the Player. If the chest has an apple and we decide to eat it, the apple has access to the interactor and can check if the interactor has an function called update_health
and if it has it, it will call it.
Method Descriptions
bool add_to_inv ( BaseItem struct, int amount )
Adds an item with the specified amount to the inventory. If successful returns true, else false. Can return false if the inventory is already full.
Array[bool, int] inv_query ( String item_name, int item_amount )
Returns if the inventory has enough of an item, and how much is the total amount.
var query = inv_query("Item1", 2)
query[0] # -> bool, is the query successful
query[1] # -> int, total amount of this item in inv
bool inv_remove_by_name ( String item_name, int item_amount )
Removes an item from the Inventory by a name
void use_item_at_slot ( int index )
Calls the i_use()
function of an item at the specified index, if the item is consumable, substracts it by 1
void refresh_slot_at_index ( int index )
Usually called by the InvSlot
, when using an item, it needs to be updated.
void swap_items_by_ref ( InvSlotStruct from_ref, InvSlotStrcut to_ref )
Used in drag and drop. Swaps two items. If you use this function, you should call refresh_slot_at_index()
after it.