Loading...
Loading...
Build a grid-based inventory UI where players can pick up, store, and use items.
Create an Item resource script. Right-click in the FileSystem → Create New → Resource. Name it 'Item.gd'. Add properties: `@export var item_name: String`, `@export var item_icon: Texture2D`, `@export var item_description: String`, `@export var max_stack: int = 1`. This defines the blueprint for all items in your game.
Create an Inventory Autoload (Singleton). Go to Project → Autoload and create 'Inventory.gd'. Add a signal: `signal inventory_updated`. Create an array to store items: `var items = []`. Add functions: `func add_item(item): items.append(item); inventory_updated.emit()` and `func remove_item(index): items.remove_at(index); inventory_updated.emit()`.
Create the inventory UI scene. Add a Control node as root, then add a GridContainer for the item slots. Set Columns to your desired grid size (e.g., 4 or 5). Add TextureRect children as slot backgrounds. Create an ItemSlot script that tracks which item is in that slot.
Connect the inventory to the UI. In your inventory UI script, connect to the Inventory.inventory_updated signal. When it emits, update the UI to show current items. Loop through inventory.items and display each item's icon in the corresponding slot.
Implement item pickup. Create an Area2D for pickupable items. When the player enters the area, detect input (e.g., 'E' key) and call Inventory.add_item(item_resource). Destroy or hide the pickupable object. The inventory_updated signal will automatically update the UI.