Here is my situation. I have a TextureButton with a texture attached. in front of it I have a label. This button is going to be used for a variety of things, what it is currently to be used for will be denoted by the text in the label. The problem is with a label in front of the button the button pressed() event doesn't fire. I see I can link an input_event(ev) to the label, but i need to do it through code and it doesn't seem to be working. I know i missing something, and any help would be great. here is some of my code.

func ready(): get_node("GroundFloor/Player2/PlayerCam/RoomEventBtn/RoomEventLbl").connect("input_event",self,"on_RoomEventBtn_pressed")

7 days later

It might be your label is blocking the mouse and the button pressed() event never gets called since the mouse click is blocked. Try setting the mouse filter to either ignore or stop on the label and that might allow the button to be pressed even though the label is on top.

(If you are using Godot 2) If you want to know when a label has been clicked, I would recommend using the 'mouse_enter()' and 'mouse_exit()' signals from control, set a boolean to true/false when the mouse has entered/exited and look for when the left/right/middle mouse button was just pressed. Then it's just a matter of checking if the boolean is true or false and doing the appropriate response.

You could do it with input_event, it would probably look something like this: (I have not tested the code below, and wrote most of it from memory and looking at the documentation)


func _ready():
  get_node(label_path_here).connect("input_event", self, "label_pressed");


func label_pressed(input_event):

   if (input_event.type == input_event.MOUSE_BUTTON):
     var label_size = get_node(label_path_here).get_size();
     var label_pos = get_node(label_path_here).get_global_pos();
     var mouse_pos = get_global_mouse_pos();
     if (label_pos <= mouse_pos <= label_pos + label_size):
        # Label clicked!

(If you are using Godot 3) I think there is a similar signal control provides for telling whether the mouse is over it. Not sure though... Using input_event would be similar, if you make the changes needed for Godot 3's new InputEvent.

Too lazy to look it up right now, but there is a flag you can set to stop input events. I'm pretty sure that it is on by default for labels. If you turn it off then anything behind it will get input events.

5 years later