Trouble with inventory system, Attempt to call function in base 'null instance' on a null instance when trying to call function from different script

Godot Version

Godot 4.3

Question

I’ve made an inventory system previously, and am now trying to make it so items can be used from various interactions, I’m trying to make my door object check to see if the correct key is in the inventory, and act accordingly, however I keep getting the error “attempt to call function ‘take_item_check’ in base ‘null instance’ on a null instance”, here’s the relevant code blocks:

Door Object

@onready var inventory = $"../UI/Inventory"

func open():
        elif variables.isLocked == true:
		        if inventory.take_item_check(1000, true) == true:
			        variables.isLocked = false

Here’s the code for the inventory

#TAKING ITEM FROM INVENTORY (EXAMPLE: HANDING IN QUEST ITEM)

#Check to see if the player has the required item in their inventory
func take_item_check(item_ID, use):
	for i in grid_array.size():
		if (grid_container.get_child(i)).stored_item_ID == item_ID:
			if use == true:
				var used_item = grid_container.get_child(i).item_stored
				take_item(used_item)
				return true
			elif use == false:
				return true
		else:
			if i == grid_array.size() - 1:
				return false
			else:
				continue


#Removing the item from the players inventory if the take_item_check function has use set to true
func take_item(used_item):
	for grid in used_item.item_grids:
		var grid_to_check = used_item.grid_anchor.slot_ID + grid[0] + grid[1] * col_count # use grid anchor instead of current slot to prevent bug
		grid_array[grid_to_check].state = grid_array[grid_to_check].States.FREE 
		grid_array[grid_to_check].item_stored = null
		grid_array[grid_to_check].stored_item_ID = null
	used_item.queue_free()

Any help is greatly appreciated!

This is because your inventory is uninitialized and empty, the problem is right from the first line of code you put in.

1 Like

I’ve referenced it up the top of the door script with

@onready var inventory = $"../UI/Inventory"

as I’ve done with other items that interact with the inventory, which has worked, what do I have to do different in this instance?

Can you put this line at the beginning of the open function and say the output? :

print(inventory) 

It returns <Object#null>

So the path of the node is not correct, if you correct it, you should get the right result.
If the path is absolutely correct, check that the inventory variable is not provisioned elsewhere or that the node is not missing.

1 Like

I added an extra step up in finding the path,

$"../../UI/Inventory"

And now instead of returning <Object#null> it’s simply returning <null>

Here’s a picture of the hierarchy. The script calling the inventory function is the highlight StaticBody3D, and it’s trying to reach the node called “Inventory”. The testkey_pickup, above the door accesses the inventory with $"../UI/Inventory" With no issue:

thing

The only idea I have for it is to address the node with the same string again at the beginning of the open, maybe somewhere in the code is missing
My perspective:

@onready var inventory = $"../UI/Inventory"

func open():
        inventory = $"../UI/Inventory"
        print(inventory) 
        elif variables.isLocked == true:
		        if inventory.take_item_check(1000, true) == true:
			        variables.isLocked = false

That worked perfectly, thank you!