Inventory & Slots¶
Inventory system is built on three main classes: Inventory (the data container), InventoryComponent (the node that attaches the data to the game world), and SlotData (the individual slot representation).
Inventory (Resource)¶
Inventory resource manages a fixed-capacity array of SlotData entries. It handles the logic for adding, removing, and stacking items, while respecting SlotDefinition rules.
Properties¶
| Property | Type | Description |
|---|---|---|
capacity |
int |
maximum number of slots in this inventory. |
slot_definitions |
Array[SlotDefinition] |
An array of rules defining what items can go into specific slots. |
slots |
Array[SlotData] |
internal array holding the actual item data. |
Signals¶
signal inventory_changed() # Emitted when the overall inventory state changes
signal slot_changed(slot_index: int) # Emitted when a specific slot is modified
signal item_added(item: ItemDefinition, count: int)
signal item_removed(item: ItemDefinition, count: int)
Key Methods¶
# Adds an item. Returns the remaining amount that could not be added (due to capacity or rules).
func add_item(item: ItemDefinition, amount: int = 1) -> int
# Removes an item. Returns true if at least some amount was successfully removed.
func remove_item(item: ItemDefinition, amount: int = 1) -> bool
# Checks if an item can be placed in a specific slot based on SlotRules and stack limits.
func can_accept_at_slot(item: ItemDefinition, slot_index: int) -> bool
# Reduces durability. If it hits 0 and 'break_on_zero' is true, the slot is cleared.
func consume_durability(item: ItemDefinition, slot_index: int, amount: int = 1) -> bool
# Utility to print the inventory state to the console for debugging.
func debug_print(label: String = "Inventory") -> void
InventoryComponent (Node)¶
InventoryComponent is the Node you attach to your Player, Chests, or Enemies to give them an inventory. It connects the Inventory resource and the game world.
Properties¶
| Property | Type | Description |
|---|---|---|
inventory |
Inventory |
actual Resource holding the data. |
capacity |
int |
Used to generate a new Inventory if create_if_missing is true. |
slot_definitions |
Array[SlotDefinition] |
Copied to the new Inventory if generated at runtime. |
create_if_missing |
bool |
If true, automatically creates an Inventory resource on _ready() if none is assigned. |
Signals & Methods¶
signal inventory_ready(inv: Inventory) # Emitted when the inventory is initialized
func get_inventory() -> Inventory # Returns the attached Inventory resource
SlotData (Resource)¶
SlotData represents a single slot within an Inventory. It holds the reference to the item, the stack count, and the current durability.
Properties¶
| Property | Type | Description |
|---|---|---|
item |
ItemDefinition |
item definition. null if the slot is empty. |
count |
int |
current stack size. |
current_durability |
int |
remaining durability. -1 means durability is not tracked. |