Inventory System: Function "set_item()" not found in base Array[InventorySlot]

Godot Version

4.2.1

Question

Hi, still new to Godot. Trying to create basic inventory system from this tutorial:

Got stuck on the: Assigning Slots for Inventory Items section.
Receiving the error:

Cannot find property "set_item" on base "Array[InventorySlot]".gdscript(-1)
Function "set_item()" not found in base Array[InventorySlot].gdscript(-1)

I thought when declaring class_name at beginning of script it makes the class global but the code doesn’t seem to find the data from the other script. What am I missing?

Code is same so far up until that section, but will paste down below mine just in case.

inventory.gd: (attached to panel node)

extends Panel
class_name Inventory

var slots : Array[InventorySlot] # Creating array called slots that holds data type InventorySlot

# Refrencing nodes so we can add functionality to them:
@onready var window : Panel = get_node("InventoryWindow")
@onready var info_text : Label = get_node("InventoryWindow/InfoText") 

@export var starter_items : Array[Item] 

# ________________________________________________________

# Defining funcs: 

func toggle_window (open : bool): 
	pass 

func on_give_player_item (item : Item , amount : int):
	pass 

func add_item (item : Item):
	pass 

func remove_item (item : Item): 
	pass

func get_slot_to_add (item : Item) -> InventorySlot:
	return null

func get_slot_to_remove (item : Item) -> InventorySlot: 
	return null

func get_number_of_item (item : Item) -> int:
	return 0


# ________________________________________________________

func _ready(): 
	toggle_window(false) # Makes certain inventory window is closed at start of scene.

	# Next we need to assign each inventory slot to the Array[InventorySlot]

	for child in get_node("SlotContainer/InventorySlot").get_children():
		slots.append(child) 
		slots.set_item(null) # Problem code...
		child.inventory = self

func _process(delta): # Called every frame.
	pass

inventory_slot.gd: (attached to button node)

extends Button
class_name InventorySlot 

# Defining Vars: (the item itself, quantity of it, inventory itself)
var quantity : int 	
var item : Item 	
var inventory : Inventory 

# reffing nodes
@onready var quantity_text : Label = get_node("QuantityText")
@onready var inventory_slot_icon : TextureRect = get_node("Icon") #*


# Defining funcs: 
func set_item(new_item : Item): #problem code...
	pass

func add_item():
	pass 

func remove_item(): 
	pass 

func update_quantity_text():
	pass 

Thanks for reading, any is help appreciated

You’re calling set_item(null) on the array itself, instead of an instance of InventorySlot.

child.set_item(null) is likely what should be there.
See below edits

for child in get_node("SlotContainer/InventorySlot").get_children():
	child.set_item(null) 
	child.inventory = self
	slots.append(child) 
1 Like

Thank you that solved it!