User Tools

Site Tools


docs:inventory

The inventory system consists of the Inventory GUI, the Player Inventory, and the Inventory Database.

Inventory GUI

Before the Inventory GUI can be used it must be connected to a PlayerInventory. You can make this connection with UI.Inventory.connect_to_inventory(new_inventory: PlayerInventory).

The Inventory GUI is hidden by default. To show it use UI.Inventory.show().

The Inventory GUI consists of 2 parts: the Hotbar and the Mainbox. Currently these cannot be hidden separately. See Issue #23.

Both the Hotbar and the Mainbox contain a number of Item UI elements.

Hotbar

The hotbar is connected to the first 10 slots of the players inventory.

Mainbox

The mainbox is connected to slots 11-40 of the player's inventory.

Item UI

These visually represent a single item in the players inventory.

Player Inventory

The Player Inventory is contained in a PlayerInventory object. You can access the current player inventory at Player.inventory.

Adding an item to inventory:

# From within player.gd
# Here we have an item named sapling
if (inventory.add_item(sapling)):
  print("Item was added successfully.")
else:
  print("Item failed to add, probably out of space")

Adding an item to a specific inventory slot:

# From within player.gd
# Overwrites item in slot 40
inventory.add_item(sapling, 39)

Swapping two item slots:

# From within player.gd
# Swap first hotbar item with first mainbox item
iventory.swap_items(0,10)

Remove an item from the inventory:

# From within player.gd
inventory.remove_item(10)

Get a reference to an inventory item:

# From within player.gd
# Will return InventoryItem or null
inventory.get(0)

Inventory Database

The Inventory Database subsystem manages the loading and defining of various types of items.

It starts by loading all of the .tres files from resources/inventory_db/items/. The resulting InventoryDBItem objects are available as a dictionary at DB.Inventory.item_list. The keys of this dictionary are named after the filename.

These InventoryDBItem objects are then used as essentially a class definition for InventoryItem objects.

InventoryDBItem

New item types can be created from the Godot GUI by right-clicking the items folder and selecting New Resource and then selecting InventoryDBItem from the menu.

You can also create new items by manually creating the .tres file:

[gd_resource type="Resource" load_steps=3 format=2]

[ext_resource path="res://scenes/databases/inventory_db/inventory_db_item.gd" type="Script" id=1]
[ext_resource path="res://assets/inventory/ITEM_ICON_FILENAME.png" type="Texture" id=2]

[resource]
script = ExtResource( 1 )
item_name = "item_name_should_match_filename"
display_name = "Pretty display name"
icon = ExtResource( 2 )
max_stack = 100

Either way the item_name property should match the name of the .tres file.

InventoryItem

InventoryItem objects represent actual items that could be placed into a player inventory slot.

To instantiate an item:

# Where apple_sapling is the name of any InventoryDBItem
var sapling: InventoryItem = InventoryItem.new("apple_sapling")

To adjust the amount:

sapling.amount = 2

To access the underlying InventoryDBItem:

# Please treat access to the db property as Read-Only
# Print the display name
print(sapling.db.display_name)

Class Reference

TODO

docs/inventory.txt · Last modified: 2021/02/04 02:59 by 94.198.42.180