Godot Version
4.2.1
Question
I’m making an inventory system which was hard (i have no idea how other games always have one) and it should work based on the tutorial, but when item gets added to the inventory the game crashes with the error “invalid get index ‘texture’ (on base ‘Dictionary’)” which is in the slots script but I doubt about it. icon.texture = item["texture"]
has a problem with this which is in the set item function.
a bit of context, there are 3 scenes, 1 for the inventory itself, 1 for the slots, 1 for items, and there is a Globaliventory script, so basically 4 scripts.
here is the slots:
func set_empty():
icon.texture=null
quantity.text=""
func set_item(new_item):
item=new_item
icon.texture = item["texture"]
quantity.text=str(item["quantity"])
item_name.text=str(item["name"])
item_type.text=str(item["type"])
if item["effect"]!="":
item_effect.text=str("+ ", item["effect"])
else:
item_effect.text=""
here is the item code:
@tool
func _process(delta):
if Engine.is_editor_hint():
icon.texture=item_texture
if player_in_range and Input.is_action_just_pressed("ui_inter"):
pickup()
func pickup():
var item={
"quantity":1,
"item_type":item_type,
"item_name":item_name,
"item_effect":item_effect,
"item_texture":item_texture,
"case_scene":case_scene
}
if Globalinventory.player_nod:
Globalinventory.add_item(item)
self.queue_free()
here is the inventory:
extends Control
func _ready():
Globalinventory.inventory_updates.connect(_update_inv)
_update_inv()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _update_inv():
clear_inventory_grid()
for item in Globalinventory.inventory:
var slots=Globalinventory.invetory_slots.instantiate()
grid.add_child(slots)
if item!=null:
slots.set_item(item)
else:
slots.set_empty()
func clear_inventory_grid():
while grid.get_child_count()>0:
var child=grid.get_child(0)
grid.remove_child(child)
child.queue_free()
here is the global:
func add_item(item):
for i in range(inventory.size()):
if inventory[i] != null and inventory[i]["effect"]==item["effect"]and inventory[i]["type"]==item["type"]:
inventory[i]["quantity"]+=item["quantity"]
inventory_updates.emit()
return true
elif inventory[i]==null:
inventory[i]=item
inventory_updates.emit()
return true
return false