Inventory System Overview
An inventory is responsible for managing owned items and their quantities.
The template provides a basic inventory.tscn
scene that can be added to any node. Its data can be saved and loaded using the Save/Load System as is already done for the player.tscn
node.
Each Inventory node allows you to assign an action_trigger
to open and close the inventory, making it accessible via a user interface.
If a UI is not required or you prefer to create your own, you can simply create a new node using the inventory.gd
script. This script provides all necessary functions to manage inventory contents, as all items are stored in the items
array.
Main Functions
is_item_in_inventory
Checks if an item is in the inventory. Returns the item index if at least one instance of the item exists. If a quantity is specified, it returns the index only if the item exists in the required amount or more.
add_item
Adds an item with the specified quantity to the inventory.
remove_item
Removes the specified quantity of an item from the inventory.
Creating a New Item
Items in this system are Godot resources. To create a new item:
- Navigate to the items folder in the FileSystem.
- Right-click and select Create New → Resource….
![Create a new resource in Godot](/_astro/godot-create-new-resource.BGOiEr_0_Z1xFi1p.webp)
- Choose
DataItem
as the resource type.
![Create a new DataItem](/_astro/create-dataitem-resource.ChduddQr_7GU2x.webp)
- Type a name for your item and click Save
![Create a new Item](/_astro/create-new-item.DHMFMC2F_2q0rC9.webp)
A DataItem
resource allows you to define:
icon
: The visual representation of the item.change_hp
: Determines whether the item affects HP (only applicable if the node has aHealthController
). Use a negative value to decrease HP and a positive value to increase it.hide_from_inventory
: If enabled, the item is managed by the inventory system but remains hidden in the UI.
![Inspecting DataItem resource](/_astro/dataitem-inspector.lfRhRtam_Z79rfx.webp)
Handling Contents
Contents are resources that track the quantity of an item. They are Godot resources of type ContentItem
.
You usually don’t need to create these manually in the FileSystem, as the inventory system handles them automatically.
The items
array in the inventory is an array of ContentItem
objects.
Other places where ContentItem
resources are used:
get_contents.gd
: Used to define a list of items and their quantities (e.g., as seen inchest.tscn
).
![Chest contents](/_astro/chest_contents.BtgfvSO9_Iahzq.webp)
StateInteract
: When configuring aCheckItems
condition to validate required inventory contents in the Conditions section.
![Check items in State Interact](/_astro/state_interact_check_items.m7VqpuVz_1hJfRD.webp)
Both DataItem
and ContentItem
can be expanded or modified to fit your
specific game needs.