Creating an inventory system in Godot 4 can be an interesting project. Below, I'll guide you through a simple way to set up an inventory system using Godot's features. This will include creating an inventory class, adding items, and displaying them in the UI.
Step 1: Create the Item Class
First, you should create a class for the items that you want to store in your inventory. Create a new script file named Item.gd.
# Item.gd
extends Resource
# Properties for an item
@export var name: String
@export var description: String
@export var icon: Texture2D
@export var id: int
Step 2: Create the Inventory Class
Next, create another script called Inventory.gd. This will manage the items in the inventory.
# Inventory.gd
extends Node
# An array to store items
var items: Array = []
# Maximum inventory size
@export var max_size: int = 20
# Function to add an item to the inventory
func add_item(item: Item) -> bool:
if items.size() < max_size:
items.append(item)
return true
return false
# Function to remove an item from the inventory
func remove_item(item: Item) -> bool:
if item in items:
items.erase(item)
return true
return false
# Function to get the list of items
func get_items() -> Array:
return items
Step 3: Set Up the User Interface
- Create a new
Control scene for your inventory UI.
- Inside that scene, add a
GridContainer or a VBoxContainer to hold item icons.
- Create a new script for this UI called
InventoryUI.gd.
Here is an example for InventoryUI.gd:
# InventoryUI.gd
extends Control
# Reference to the inventory
@onready var inventory: Inventory = $Inventory
func _ready():
update_inventory()
# Function to update the displayed inventory
func update_inventory():
# Clear existing items from the UI
for child in get_children():
if child is TextureRect:
child.queue_free() # Remove any old item icons
# Loop through inventory items and create icons
for item in inventory.get_items():
var item_icon = TextureRect.new()
item_icon.texture = item.icon
item_icon.size_flags_horizontal = Control.SIZE_EXPAND_FILL
item_icon.size_flags_vertical = Control.SIZE_EXPAND_FILL
add_child(item_icon)
Step 4: Integrating Everything
- Create an instance of the
Inventory in your main game scene, for example in Game.gd.
# Game.gd
extends Node
@onready var inventory: Inventory = Inventory.new()
@onready var inventory_ui: InventoryUI = preload("res://path/to/InventoryUI.tscn").instance()
func _ready():
add_child(inventory)
add_child(inventory_ui)
# Create and add example items to the inventory
var item1 = preload("res://path/to/Item.tres").instantiate()
item1.name = "Health Potion"
item1.description = "Restores health."
item1.icon = preload("res://path/to/icon.png") # Adjust to your icon path
inventory.add_item(item1)
inventory_ui.update_inventory()
Step 5: Testing
- Make sure to create some
Item.tres resources for your items and link them properly.
- Implement a process to add and remove items from the inventory. You could do this based on game events or player actions (like picking up items).
Summary
This is a basic setup for an inventory system in Godot 4. You can expand this by adding features like item stacking, tooltips, drag-and-drop functionality, or saving/loading the inventory state. The approach used here is modular, making it easier to enhance as your game grows!