Get_parent().get_index() returns null

v4.2.1.stable.official [b09f793f5]

I have a hotbar and I’m trying to change the slot colour on mouse click.
The hotbar instantiates the slot scene, wherein I have the following code:

func _on_slot_sprite_mouse_entered(): 
	mouse_index = get_parent().get_index()
	print(mouse_index)


func _input(event):
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		current_index = mouse_index
	highlight(current_index)


func highlight(index):
	if get_parent().get_parent().get_child(index).get_name() == slot_name:
		button_pressed = true
	else:
		button_pressed = false

This prints the slots index on mouse-over, but returns null on mouse-click. I don’t get it.
The node instantiating the slot is a GridContainer. The slot itself is a MarginContainer with a TextureButton as its child. This is where the script is placed.
Any help would be appreciated.

all of the slots will have _input() called every time that you click, you’re probably looking for _gui_input() instead.

One of the other slots saw the mouse click but hadn’t yet been hovered over.

You probably don’t want to handle determining which is the “current” index of the hotbar inside the individual items, that should be done by the hotbar itself. the items all have their own mouse_index and current_index property so it’ll just be a mess to try to get them all to agree with each other.

try connecting to the gui_input signal of your individual slot items from the hotbar when you instantiate them, and bind the slot itself to your callback so you know which one just got clicked

real quick, rough example

var new_slot: = slot.instantiate()
new_slot.gui_input.connect(_slot_gui_input.bind(new_slot))

func _slot_gui_input(event: InputEvent, the_slot) -> void:
  if event.is_mouse_button_pressed(..LEFT):
    the_slot.button_pressed = true

Probably don’t call it button_pressed if it’s actually a MarginContainer, but that’s the general idea

1 Like

Thank you for the help.