![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | The Slurge |
Hello, I’m following a Youtube tutorial on how to design an inventory system. I’m not getting any errors, however I can’t get anything to appear in the player’s inventory. It should go like this:
- Player moves to area and clicks a button
- A new child of the label type gets added to a grid container which is attached to the player
- Grid container should display all items currently in the inventory.
The problem is that I can’t get the grid container to add any labels, even though items seem to be getting added to the _items array. When I try printing the array after adding an item I get this output:
[{item_reference:[Resource:1308], quantity:50}
that appears about 100 times or so.
Inventory script:
extends Resource
class_name Inventory
signal inventory_changed
export var _items = Array() setget set_items, get_items
func set_items(new_items):
_items = new_items
emit_signal(“inventory_changed”, self)
func get_items():
return _items
func get_item(index):
return _items[index]
func add_item(item_name, quantity):
if quantity <= 0:
print(“Can’t add less than 0 items, ya schmuck!”)
return
var item = ItemDatabase.get_item(item_name)
if not item:
print("Could not find item, ya schmuck!")
return
var remaining_quantity = quantity
var max_stack_size = item.max_stack_size if item.stackable else 1
if item.stackable:
for i in range(_items.size()):
if remaining_quantity == 0:
break
var inventory_item = _items[i]
if inventory_item.item_reference.name != item.name:
continue
if inventory_item.quantity < max_stack_size:
var original_quantity = inventory_item.quantity
inventory_item.quantity = min(original_quantity + remaining_quantity, max_stack_size)
remaining_quantity -= inventory_item.quantity - original_quantity
while remaining_quantity > 0:
var new_item = {
item_reference = item,
quantity = min(remaining_quantity, max_stack_size)
}
_items.append(new_item)
remaining_quantity -= new_item.quantity
emit_signal("inventory_changed", self)
print(_items)
GameManager script:
extends Node
signal player_initialised
var player
func _ready():
if not player:
initialise_player()
return
func initialise_player():
player = get_tree().get_root().get_node(“/root/World/Player”)
if not player:
print(“That player path was wrong, ya schmuck!”)
return
emit_signal("player_initialised", player)
player.inventory.connect("inventory_changed", self, "_on_player_inventory_changed")
var existing_inventory = load("user://inventory.tres")
if existing_inventory:
player.inventory.set_items(existing_inventory.get_items())
func _on_player_inventory_changed(inventory):
ResourceSaver.save(“user://inventory.tres”, inventory)
InventoryGrid script:
extends GridContainer
func _ready():
GameManager.connect(“player_initialised”, self, “_on_player_initialised”)
print(“here”)
func _on_player_initialised(player):
player.inventory.connect(“inventory_changed”, self, “_on_player_inventory_changed”)
func _on_player_inventory_changed(inventory):
for n in get_children():
n.queue_free()
for item in inventory.get_items():
var item_label = Label.new()
item_label.text = "%s x%d" % [item.item_reference.name, item.quantity]
add_child(item_label)
ItemDatabase script:
extends Node
var items = Array()
func _ready():
var directory = Directory.new()
directory.open(“res://Items/”)
directory.list_dir_begin()
var file_name = directory.get_next()
while(file_name):
if not directory.current_is_dir():
items.append(load("res://Items/%s" % file_name))
file_name = directory.get_next()
func get_item(item_name):
for i in items:
if i.name == item_name:
return i
return null
Item script
extends Resource
class_name ItemResource
export var name : String
export var stackable : bool = true
export var max_stack_size : int = 1
I’m sorry if my blockquotes aren’t including all my code, I am not sure how to fix it. Thanks in advance!
The code looks OK to me. Are you sure that the Grid is visible such as it’s height/width is not zero for example?
Andrew Wilkes | 2022-08-29 07:41
That could be the issue! How would I check that? Would I hardcode the dimensions of the label somehow?
The Slurge | 2022-09-02 17:20
Sorry, I misread your question. Currently I have a control node with a child of type panel set to fill rect and a grid container as the child of panel. I’ll make sure that I have the grid container’s size set properly. Thanks for the suggestion!
The Slurge | 2022-09-02 18:12