I have an if statement that goes like this:

	if ItemSlots.get_node_or_null("Item1Slot/SubwayTicket").is_pressed(): 
		 InventoryItem.ItemName = "SubwayTicket"
		 Use.disabled = false

As you can see, I used get_node_or_null so that it doesn't display an error when the node is not found. I used this because at a certain point in the game, the node gets deleted (using queue_free()).
The issue is that when the node gets deleted, it still displays an error saying the following:

Attempt to call function 'is_pressed' in base 'null instance' on a null instance.

My guess is that this happens because it's attempting to execute is_pressed() on a node that doesn't exist. My question is: How can I stop the game from executing is_pressed() when the node is null, when it doesn't exist anymore?

  • Use something like this:

    var subway_ticket: Node = ItemSlots.get_node_or_null("Item1Slot/SubwayTicket")
    if is_instance_valid(subway_ticket) and subway_ticket.is_pressed():

Use something like this:

var subway_ticket: Node = ItemSlots.get_node_or_null("Item1Slot/SubwayTicket")
if is_instance_valid(subway_ticket) and subway_ticket.is_pressed():