Inventory

Inherits: NodeInventories < Node

Inherited By: GridInventory

This node is a stack container.

Description

Each stack contains data about amount, ItemStack(with String item_id from InventoryDatabase) and custom properties.

Stacks are stored in an Array, which can be accessed with inventory.stacks, but it is best to avoid changing data directly and instead use methods within this node, such as add(), remove(), set_stack_content(), …

The methods correctly call signals when necessary.

Some methods have their general version and a direct version of some Stack index, such as contains() and contains_at().

Tutorials

Properties

Array[InventoryConstraint]

constraints

[]

String

inventory_name

"Inventory"

Array[ItemStack]

stacks

[]

Methods

int

add(item_id: String, amount: int = 1, properties: Dictionary = {}, drop_excess: bool = false)

int

add_at_index(stack_index: int, item_id: String, amount: int = 1, properties: Dictionary = {})

int

add_on_new_stack(item_id: String, amount: int = 1, properties: Dictionary = {}, can_emit_signal: bool = true)

int

add_to_stack(stack: ItemStack, item_id: String, amount: int = 1, properties: Dictionary = {})

int

amount_of_item(item_id: String) const

bool

can_add_new_stack(item_id: String, amount: int = 1, properties: Dictionary = {}) const

void

clear()

bool

contains(item_id: String, amount: int = 1) const

bool

contains_at(stack_index: int, item_id: String, amount: int = 1) const

bool

contains_category(category: ItemCategory, amount: int = 1) const

bool

contains_category_in_stack(stack: ItemStack, category: ItemCategory) const

void

deserialize(data: Dictionary)

bool

drop(item_id: String, amount: int = 1, properties: Dictionary = {})

void

drop_all_stacks()

void

drop_from_inventory(stack_index: int, amount: int = 1, properties: Dictionary = {})

int

get_amount() const

int

get_amount_of_category(category: ItemCategory) const

int

get_stack_index_with_an_item_of_category(category: ItemCategory) const

bool

has_space_for(item: String, amount: int = 1, properties: Dictionary = {}) const

bool

has_stack(stack: ItemStack) const

bool

is_empty() const

bool

is_full() const

int

remove(item_id: String, amount: int = 1)

int

remove_at(stack_index: int, item_id: String, amount: int = 1)

int

remove_from_stack(stack: ItemStack, item_id: String, amount: int = 1)

void

remove_stack(stack_index: int)

Dictionary

serialize() const

void

set_stack_content(stack_index: int, item_id: String, amount: int = 1, properties: Dictionary = {})

bool

split(stack_index: int, amount: int = 1)

int

transfer(stack_index: int, destination: Inventory, amount: int = 1)

int

transfer_at(stack_index: int, destination: Inventory, destination_stack_index: int, amount: int = 1)

void

update_stack(stack_index: int)


Signals

contents_changed() 🔗

Emitted when the contents of the inventory change.

This signal is emitted after the remove(), remove_at(), set_stack_content(), add(), add_at() or update_stack() function.


emptied() 🔗

Emitted when inventory is empty. This signal is emitted after the remove(), remove_at() or set_stack_content() function and it only happens when there are no stacks.


filled() 🔗

Emitted when inventory is filled. This signal is emitted after the add(), add_at() or set_stack_content() function and it only happens when all stacks are filled.


item_added(item_id: String, amount: int) 🔗

Emitted when a Item was added.


item_removed(item_id: String, amount: int) 🔗

Emitted when a Item was removed.


request_drop_item(item_id: String, amount: int, item_properties: Dictionary) 🔗

Emitted when the drop() method is called (Usually called within drop_from_inventory() or drop_all_stacks()).


stack_added(stack_index: int) 🔗

Emitted when a new stack is added to the inventory.

This signal is emitted after the add(), add_at() or add_new_stack() function.


stack_removed(stack_index: int) 🔗

Emitted when a stack is removed from the inventory.

This signal is emitted after the remove(), remove_at() or remove_stack() function.


updated_stack(stack_index: int) 🔗

Emitted when a stack is updated.

This signal is emitted after the update_stack() function.


Property Descriptions

Array[InventoryConstraint] constraints = [] 🔗

Resources that extend InventoryConstraint and define rules about inventory additions.


String inventory_name = "Inventory" 🔗

  • void set_inventory_name(value: String)

  • String get_inventory_name()

The name of the inventory, to be displayed in UI.


Array[ItemStack] stacks = [] 🔗

ItemStack list, which stores three pieces of information: item_id, amount and dynamic properties of the items.


Method Descriptions

int add(item_id: String, amount: int = 1, properties: Dictionary = {}, drop_excess: bool = false) 🔗

Adds an amount of ItemStack to the inventory and returns the amount that cannot be added.

There are cases where the inventory may be full or does not accept this item by its category, so the return of this call corresponds to the quantity that was not added (If it returns 0 it means that all items were added correctly)


int add_at_index(stack_index: int, item_id: String, amount: int = 1, properties: Dictionary = {}) 🔗

Adds an amount of ItemStack(with string id from InventoryDatabase) to a specific inventory stack index and returns the amount that cannot be added.


int add_on_new_stack(item_id: String, amount: int = 1, properties: Dictionary = {}, can_emit_signal: bool = true) 🔗

Add item_id to a new stack, ignoring the possibility of adding that item to an existing stack.


int add_to_stack(stack: ItemStack, item_id: String, amount: int = 1, properties: Dictionary = {}) 🔗

Adds an amount of item_id to the specified stack and returns the amount that cannot be added.


int amount_of_item(item_id: String) const 🔗

Returns amount of the specified ItemStack.


bool can_add_new_stack(item_id: String, amount: int = 1, properties: Dictionary = {}) const 🔗

Returns true if it is possible to add a new stack to the inventory, note that this method consults the configured constraints.


void clear() 🔗

Clears the inventory, removing all stacks.


bool contains(item_id: String, amount: int = 1) const 🔗

Returns true if contains the amount of the specified ItemStack(with String item_id from InventoryDatabase).


bool contains_at(stack_index: int, item_id: String, amount: int = 1) const 🔗

Returns true if the stack index contains the amount of the specified ItemStack(with String item_id from InventoryDatabase).


bool contains_category(category: ItemCategory, amount: int = 1) const 🔗

Returns true if contains the amount of ItemStack(with String item_id from InventoryDatabase) with the specified ItemCategory.


bool contains_category_in_stack(stack: ItemStack, category: ItemCategory) const 🔗

Returns true if the stack contains the amount of item (with String item_id from InventoryDatabase) with the specified ItemCategory.


void deserialize(data: Dictionary) 🔗

Deserializes the inventory with a previously serialized dictionary by rewriting the data of this inventory.


bool drop(item_id: String, amount: int = 1, properties: Dictionary = {}) 🔗

Drop the specified amount of item(with String item_id from InventoryDatabase) from inventory and returns true if it was successful.


void drop_all_stacks() 🔗

Drop all stacks on inventory, request_drop_obj signal is emitted for each stack.


void drop_from_inventory(stack_index: int, amount: int = 1, properties: Dictionary = {}) 🔗

Drop the stack with the specified index.


int get_amount() const 🔗

Returns amount of items.


int get_amount_of_category(category: ItemCategory) const 🔗

Returns amount of the specified ItemCategory.


int get_stack_index_with_an_item_of_category(category: ItemCategory) const 🔗

Returns the index of the first stack that contains an item of the specified ItemCategory.

If there is no stack with an item of this category, it returns -1.


bool has_space_for(item: String, amount: int = 1, properties: Dictionary = {}) const 🔗

Returns true if the inventory has space for the specified ItemStack(with String item_id from InventoryDatabase).

Note: This method check the constraints too.


bool has_stack(stack: ItemStack) const 🔗

Returns true if the inventory has a stack with the specified ItemStack.


bool is_empty() const 🔗

Returns true if inventory is empty.


bool is_full() const 🔗

Returns true if inventory is full.


int remove(item_id: String, amount: int = 1) 🔗

Removes a amount of the ItemStack(with String item_id from InventoryDatabase) from inventory and returns the amount that was not removed.


int remove_at(stack_index: int, item_id: String, amount: int = 1) 🔗

Removes an item_id amount to the specified inventory ItemStack index and returns the remaining value that was not removed.


int remove_from_stack(stack: ItemStack, item_id: String, amount: int = 1) 🔗

Remove stack with stack parameter, set emit_signal to false to disable events called by update_stack().


void remove_stack(stack_index: int) 🔗

Removes the stack with the specified index.


Dictionary serialize() const 🔗

Serializes the inventory returning a dictionary with inventory information.


void set_stack_content(stack_index: int, item_id: String, amount: int = 1, properties: Dictionary = {}) 🔗

Sets the content of the stack with the specified index.


bool split(stack_index: int, amount: int = 1) 🔗

Splits the stack at the specified index and returns true if it was successful.


int transfer(stack_index: int, destination: Inventory, amount: int = 1) 🔗

Transfer the contents of the stack_index stack_index of this inventory to another inventory (destination).

Note: If amount is -1 (default value), the entire contents of the stack are sent to another inventory.


int transfer_at(stack_index: int, destination: Inventory, destination_stack_index: int, amount: int = 1) 🔗

Transfer the contents of the stack_index stack_index of this inventory to another stack_index (destination_stack_index) of destination Inventory.

Note: If amount is -1 (default value), the entire contents of the stack are sent to another inventory.


void update_stack(stack_index: int) 🔗

Updates the stack with the specified index.

This method is called automatically when a stack is added or removed, but it can also be called manually to update the stack.

Note: This method emits the updated_stack signal.