Skip to content
TheYellowArchitect edited this page Sep 8, 2024 · 16 revisions

This tutorial teaches you how to work with the Inventory node.

  1. Setup

  2. Add Items to Inventory

Setup

Let's start with an empty scene

image

Add an inventory node, the inventory node is the main structure of this addon.

Note: Understanding the basics is important to understand the flexibility of this addon.

godot windows opt tools 64_FWHxsDbJGb

After adding the node, it will display a warning indicating that it needs to configure its database.

Note: The inventoryDatabase is where the item structures and their attributes will be saved, important data, for example, an item's identifier, are contained within this resource.

This tutorial does not cover the database, we will use the database already available in the demo, to understand more about Inventory Database, check the tutorials about it.

image

Define the database, it should look like this.

image

In the node inspector, we can see the current inventory slots, we will use it to view what is available within the inventory. Don't change anything now.

image

Create a test script on the parent node of the inventory node, it will be used for all our tests from now on.

godot windows opt tools 64_elGgAzjtnD

Add Items to Inventory

Let's create a variable pointing to the inventory, dragging it to the script while holding the control key.

godot windows opt tools 64_emNvmbmg6P

We added code to display slots each time we click the configured action "interact" which in the demo is the E key

func _process(delta):
	if Input.is_action_just_pressed("interact"):
		print("Inventory Slots:")
		for slot in inventory.slots:
			print("A Slot")

The result is this:

Inventory Slots:
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot
A Slot

Modify the code now to display when there is an item in the slot, its name and quantity, if not it displays the message "Empty"

func _process(delta):
	if Input.is_action_just_pressed("interact"):
		print("Inventory Slots:")
		for slot in inventory.slots:
			if slot.item != null:
				print(slot.item.definition.name," x ", slot.amount)
			else:
				print("Empty")

Let's configure an item to be added, add a variable to link an item to the script, so we can add it to the inventory:

...
@onready var inventory = $Inventory

# Add this 👇
@export var item : Item

# Called when the node enters the scene tree for the first time.
func _ready():
...

Place an item in the inspector, here we place the 'wood' item that is in the demo database image

We added code that adds an item each time we click the Q key

			print(slot.item.definition.name," x ", slot.amount)
			else:
				print("Empty")
        # Add this 👇
	if Input.is_action_just_pressed("add_item_a"):
		inventory.add(item, 1)

Now after typing F1 (key that adds item configured in the demo) and then E (interaction key configured in the demo), you can see that an item has been added to the inventory.

Inventory Slots:
Wood x 1
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty
Empty