Skip to content

InventoryComponent ​

Inherits: RefCounted

Universal inventory system for storing item instances and currency

Description ​

This class provides pure inventory functionality that works universally across:

  • Player inventories
  • NPC inventories
  • Container inventories (chests, bags, etc.)
  • Any other item storage system

UI behavior and context are handled by the UI layer, not here. For specialized trading behavior, see VendorInventoryComponent.

Variables ​

Dictionarycurrency{}
Array[InventorySlotInstance]slots[]
intmax_slots20
CombatManagercombat_manager
boolis_sharedfalse
InventoryComponentwalletnull
InventoryStylestylenull
Entityowner_entitynull
intextra_slots0
floatextra_capacity0.0
Stringlast_block_reason""
Array[InventorySlotInstance]aside_slots[]

Methods ​

voidconfigure( p_style: InventoryStyle, p_owner: Entity = null, slots_override: int = -1 )
intget_slot_capacity()
voidrefresh_capacity()
Array[ItemInstance]get_worn_bags()
intget_overflow_count()
boolcan_release_bag( bag_item: ItemInstance )
voidadd_extra_slots( amount: int )
voidadd_extra_capacity( amount: float )
boolis_grid()
intget_grid_width()
Vector2iget_grid_cell( slot: InventorySlotInstance )
intcount_grid_placements( shape: Vector2i, wanted: int )
boolcan_place_in_grid( item_instance: ItemInstance, target_slot: InventorySlotInstance )
boolplace_in_grid( item_instance: ItemInstance, target_slot: InventorySlotInstance )
boolis_weight_on()
floatget_total_weight()
floatget_weight_capacity()
floatget_weight_percent()
boolis_over_capacity()
voidrefresh_encumbrance()
EncumbranceTierget_encumbrance_step()
voidattach_wallet( shared_wallet: InventoryComponent )
voidinitialize_inventory( slot_count: int = 20 )
voidsetup_inventory( inventory: Dictionary )
booladd_item( item_instance: ItemInstance )
booltry_swap_items( item_instance: ItemInstance, target_slot_index: int )
booladd_item_definition( item_def: ItemDefinition, quantity: int = 1 )
boolhas_room_for_definition( item_def: ItemDefinition, quantity: int )
intadd_item_definition_partial( item_def: ItemDefinition, quantity: int )
boolremove_item( item_instance: ItemInstance, quantity: int = -1 )
boolremove_item_definition( item_def: ItemDefinition, quantity: int )
intremove_item_definition_partial( item_def: ItemDefinition, quantity: int )
voidclear_inventory()
intget_item_count( item_instance: ItemInstance )
boolhas_item( item_instance: ItemInstance, required_quantity: int = 1 )
Array[ItemInstance]get_items( item_instance: ItemInstance )
boolhas_space_for_item( item_instance: ItemInstance )
intget_item_count_definition( item_def: ItemDefinition )
boolhas_item_definition( item_def: ItemDefinition, required_quantity: int = 1 )
Array[ItemInstance]get_items_definition( item_def: ItemDefinition )
Array[ItemInstance]get_all_items()
ItemInstanceget_item_at_index( index: int )
intget_index_of_slot( slot_instance: InventorySlotInstance )
InventorySlotInstancefind_slot_with_item( item_instance: ItemInstance )
Array[InventorySlotInstance]get_empty_slots( definition: ItemDefinition = null )
boolis_completely_full()
boolis_full()
boolis_empty()
intget_currency_amount( currency_id: int )
intget_currency_limit( currency_id: int )
voidset_currency_amount( currency_id: int, amount: int )
intadd_currency_clamped( currency_id: int, amount: int )
booladd_currency( currency_id: int, amount: int )
boolremove_currency( currency_id: int, amount: int )
boolcan_afford_currency( currency_id: int, amount: int )
boolspend_currency( currency_id: int, amount: int )
booladd_currency_definition( currency_def: CurrencyDefinition, amount: int )
boolremove_currency_definition( currency_def: CurrencyDefinition, amount: int )
boolcan_afford_currency_definition( currency_def: CurrencyDefinition, amount: int )
boolspend_currency_definition( currency_def: CurrencyDefinition, amount: int )
Dictionaryget_all_currencies()
Dictionaryget_visible_currencies()
Dictionaryget_all_currency_amounts()
voidclear_all_currency()
floatadd_item_with_quantity( item_instance: ItemInstance, quantity: float )
Dictionarycalculate_addable_quantity( item_instance: ItemInstance, desired_quantity: float )
boolcan_discard_item( item_instance: ItemInstance )
booldiscard_item( item_instance: ItemInstance, quantity: int = -1 )
Array[ItemInstance]get_aside_items()
intremove_completed_quest_items()
intremove_quest_items_for_quest( quest_id: int )
intremove_quest_items_on_end( quest_id: int, completed: bool )
intremove_quest_items_on_completion( quest_id: int )
boolhas_quest_items_for_quest( quest_id: int )
Array[ItemInstance]get_all_quest_items()
Array[ItemInstance]get_quest_items_for_quest( quest_id: int )
Dictionaryto_save_data()
voidfrom_save_data( save_data: Dictionary )

Signals ​

inventory_updated() ​

Emitted whenever the inventory contents change

item_added( item_instance: ItemInstance, slot_index: int ) ​

Emitted when an item instance is successfully added to inventory

item_removed( item_instance: ItemInstance, quantity: int ) ​

Emitted when an item instance is successfully removed from inventory

currency_changed( currency_id: int, new_amount: int ) ​

Emitted when currency amounts change

new_currency_added( currency_id: int, initial_amount: int ) ​

Emitted when a new currency type is added to the inventory for the first time

slots_resized() ​

Emitted when the number of slots changed (the window builds its slots again)

Variable descriptions ​

Dictionary currency = ​

Currency storage - currency_id (int) -> amount (int)

Array[InventorySlotInstance] slots = [] ​

Array of inventory slots containing item instances

int max_slots = 20 ​

Maximum number of inventory slots available

CombatManager combat_manager ​

The combat system manager

bool is_shared = false ​

This is the one bag of the whole party (the characters all use this object): its changes are not the business of one character alone

InventoryComponent wallet = null ​

The purse of the party, when the money is shared: the currency functions of this inventory work on it, so the bag of every member shows and spends the same money

InventoryStyle style = null ​

The rules of this inventory (space, weight). Null = a plain list of max_slots slots, as before

Entity owner_entity = null ​

The entity whose bag this is (its stats give the capacity, its effects are used for encumbrance), if any

int extra_slots = 0 ​

Slots added for good (a reward, an event): they come on top of what the style gives

float extra_capacity = 0.0 ​

Weight capacity added for good

String last_block_reason = "" ​

Why the last add was held back ("" = it was not): the giver can tell the player

Array[InventorySlotInstance] aside_slots = [] ​

The stacks that are kept aside: items that take no space (no slot, no cell, no weight), see ItemDefinition.is_kept_aside. They are a list outside slots that grows and shrinks with what is in it, and they count everywhere else (counts, removal, requirements, triggers, saves)

Method descriptions ​

void configure( p_style: InventoryStyle, p_owner: Entity = null, slots_override: int = -1 ) ​

Sets the rules of this inventory, whose it is, and (for a container) the number of slots it asks for. The size follows the stats from now on

int get_slot_capacity() ​

How many slots the rules give now: the style's, plus the slots added for good. A plain inventory (no style) has max_slots

void refresh_capacity() ​

Makes the slots what the rules say: the slots of the style (the stat, the bonuses) and the slots of the bags worn. Growing adds slots; shrinking never destroys items: an item in a slot that goes away moves to a free slot, and when there is none its slot stays (over the limit) until it is emptied

Array[ItemInstance] get_worn_bags() ​

The bags the owner wears, in the order of the equipment slots

int get_overflow_count() ​

Slots of the character's own bag that hold items past the capacity (the bag got smaller and there was nowhere to move them)

bool can_release_bag( bag_item: ItemInstance ) ​

Could this bag come off? What it holds has to fit in the other slots (and the bag itself needs a slot too)

void add_extra_slots( amount: int ) ​

Adds slots for good (a reward, an event). A negative number takes them away again

void add_extra_capacity( amount: float ) ​

Adds weight capacity for good

bool is_grid() ​

Is this a grid inventory (and not a shared bag)?

int get_grid_width() ​

How many cells wide the grid is

Vector2i get_grid_cell( slot: InventorySlotInstance ) ​

The cell of a slot of a grid (x across, y down)

int count_grid_placements( shape: Vector2i, wanted: int ) ​

How many items of this shape fit side by side in the free cells (up to wanted)

bool can_place_in_grid( item_instance: ItemInstance, target_slot: InventorySlotInstance ) ​

Does this item fit with its top left cell on this slot? (the item may be in the grid already: its own cells do not count)

bool place_in_grid( item_instance: ItemInstance, target_slot: InventorySlotInstance ) ​

Puts the item with its top left cell on this slot: it moves there if it fits, or joins the stack that is there. False when it does not fit

bool is_weight_on() ​

Does this inventory count weight?

float get_total_weight() ​

What everything in the bag weighs (and what is worn, when the style counts it)

float get_weight_capacity() ​

The most this inventory can carry before the limit

float get_weight_percent() ​

What share of the capacity is carried (100 = exactly the capacity); 0 when there is no capacity to compare with

bool is_over_capacity() ​

Is the load above the capacity?

void refresh_encumbrance() ​

Puts the effect of the step of encumbrance that the load has reached on the owner (and takes the old one off)

EncumbranceTier get_encumbrance_step() ​

The step of encumbrance that is on now (null = none)

void attach_wallet( shared_wallet: InventoryComponent ) ​

Uses the purse of another inventory(the shared purse of the party) for all money, from now on. The money this one held is moved into it

void initialize_inventory( slot_count: int = 20 ) ​

Initialize the inventory with the specified number of slots

void setup_inventory( inventory: Dictionary ) ​

Setup inventory from a dictionary

bool add_item( item_instance: ItemInstance ) ​

Add item instance to inventory, returns true if successful

bool try_swap_items( item_instance: ItemInstance, target_slot_index: int ) ​

Moves an item to a slot, swapping it with what is there. False when the slot is not valid.

bool add_item_definition( item_def: ItemDefinition, quantity: int = 1 ) ​

Add item by definition: all of it or nothing (a quantity above one stack makes several stacks; with no room for all of it nothing is added)

bool has_room_for_definition( item_def: ItemDefinition, quantity: int ) ​

Is there room for this many of an item (merging into stacks and using empty slots)?

int add_item_definition_partial( item_def: ItemDefinition, quantity: int ) ​

Add item by definition with partial success (returns remaining quantity that couldn't be added)

bool remove_item( item_instance: ItemInstance, quantity: int = -1 ) ​

Remove specific item instance from inventory

bool remove_item_definition( item_def: ItemDefinition, quantity: int ) ​

Remove items by definition (removes from any instances of this item type)

int remove_item_definition_partial( item_def: ItemDefinition, quantity: int ) ​

Remove items by definition and return how many were actually removed

void clear_inventory() ​

Empties every slot.

int get_item_count( item_instance: ItemInstance ) ​

Get total count of a specific item instance type

bool has_item( item_instance: ItemInstance, required_quantity: int = 1 ) ​

Check if inventory contains at least the required quantity of a specific item instance type

Array[ItemInstance] get_items( item_instance: ItemInstance ) ​

Get all instances of the same type as the provided item instance

bool has_space_for_item( item_instance: ItemInstance ) ​

Check if inventory has space for a specific item instance

int get_item_count_definition( item_def: ItemDefinition ) ​

Get total count of item instances by definition

bool has_item_definition( item_def: ItemDefinition, required_quantity: int = 1 ) ​

Check if inventory contains at least the required quantity of an item definition

Array[ItemInstance] get_items_definition( item_def: ItemDefinition ) ​

Get all item instances of a specific definition

Array[ItemInstance] get_all_items() ​

Get all item instances in inventory

ItemInstance get_item_at_index( index: int ) ​

Get item at specific slot index

int get_index_of_slot( slot_instance: InventorySlotInstance ) ​

Get the index of a specific slot instance

InventorySlotInstance find_slot_with_item( item_instance: ItemInstance ) ​

Find first slot containing an item instance

Array[InventorySlotInstance] get_empty_slots( definition: ItemDefinition = null ) ​

Get all empty slots

bool is_completely_full() ​

Is there no room at all? No free slot (or cell, or room to grow) and no stack of an item that stacks that has room left. A bag like this takes nothing

bool is_full() ​

Check if inventory is full

bool is_empty() ​

Check if inventory is empty

int get_currency_amount( currency_id: int ) ​

Get the amount of a specific currency

int get_currency_limit( currency_id: int ) ​

The most of a currency that can be held (0 = no limit): the maximum of its definition

void set_currency_amount( currency_id: int, amount: int ) ​

Set the amount of a specific currency (never below 0, never above the maximum of the currency)

int add_currency_clamped( currency_id: int, amount: int ) ​

Add currency and say how much went in (the excess over the maximum of the currency is lost)

bool add_currency( currency_id: int, amount: int ) ​

Add currency to inventory (true when something went in)

bool remove_currency( currency_id: int, amount: int ) ​

Remove currency from inventory

bool can_afford_currency( currency_id: int, amount: int ) ​

Check if inventory has enough currency

bool spend_currency( currency_id: int, amount: int ) ​

Spend currency if available

bool add_currency_definition( currency_def: CurrencyDefinition, amount: int ) ​

Add currency by definition

bool remove_currency_definition( currency_def: CurrencyDefinition, amount: int ) ​

Remove currency by definition

bool can_afford_currency_definition( currency_def: CurrencyDefinition, amount: int ) ​

Check if can afford currency by definition

bool spend_currency_definition( currency_def: CurrencyDefinition, amount: int ) ​

Spend currency by definition

Dictionary get_all_currencies() ​

Get all currency types in inventory

Dictionary get_visible_currencies() ​

The currencies the player is shown: the ones held, without the ones that are not listed with the money (CurrencyDefinition.show_in_wallet)

Dictionary get_all_currency_amounts() ​

Get all currency amounts in inventory (alias for compatibility)

void clear_all_currency() ​

Clear all currency

float add_item_with_quantity( item_instance: ItemInstance, quantity: float ) ​

Add item with quantity, returns remaining quantity that couldn't be added

Dictionary calculate_addable_quantity( item_instance: ItemInstance, desired_quantity: float ) ​

Calculate how much of an item can be added without actually adding it

bool can_discard_item( item_instance: ItemInstance ) ​

A key item cannot be discarded (dropped, destroyed or sold)

bool discard_item( item_instance: ItemInstance, quantity: int = -1 ) ​

Throw an item away (a stack, or part of it). False for a key item

Array[ItemInstance] get_aside_items() ​

The items that are kept aside (they take no space in the bag)

int remove_completed_quest_items() ​

Remove all quest items that should be auto-removed (quest completed) Call this when a quest is completed to clean up associated items Returns the number of item stacks removed

int remove_quest_items_for_quest( quest_id: int ) ​

Remove all quest items for a specific quest ID Useful when you want to remove items for a specific quest Returns the number of item stacks removed

int remove_quest_items_on_end( quest_id: int, completed: bool ) ​

Remove the items of a quest that stopped running: the ones that only exist while it runs, and (when it completed) the ones made to leave with it (remove_on_quest_complete). Returns the number of stacks removed

int remove_quest_items_on_completion( quest_id: int ) ​

Remove the items of a completed quest that are made to leave with it (kept for scripts; see remove_quest_items_on_end)

bool has_quest_items_for_quest( quest_id: int ) ​

Check if inventory contains any quest items for a specific quest

Array[ItemInstance] get_all_quest_items() ​

Get all quest items in the inventory

Array[ItemInstance] get_quest_items_for_quest( quest_id: int ) ​

Get all quest items for a specific quest ID

Dictionary to_save_data() ​

Serialize inventory state to dictionary for saving

void from_save_data( save_data: Dictionary ) ​

Restore inventory state from saved dictionary data