func _on_label_pressed(label) does not work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mavitaka

Hello,
I dont know why func _on_label_pressed(label) in my code does not work.
Could you please help?
Thank you in advance for your reply.
Best regards.

extends Control

var numbers = []

func _ready():
	randomize()
	for i in range(1, 101):
		var label = Label.new()
		label.text = str(i)
		label.rect_position = Vector2(rand_range(0, get_viewport_rect().size.x), rand_range(0, get_viewport_rect().size.y))
		label.connect("mouse_pressed", self, "_on_label_pressed", [label])
		label.mouse_filter = Control.MOUSE_FILTER_STOP
		add_child(label)
		numbers.append(label)
		
func _on_label_pressed(label):
	print("Label clicked:", label.text)
:bust_in_silhouette: Reply From: AlexTheRegent

There is no such signal as mouse_pressed. There is signal named pressed, but it is available at Button class, not Label. You can easily turn Label into Button changing all of its 5 styles to “StyleBoxEmpty” (Theme Overrides/Styles). Or if really have to use Label, you can go with some workarounds, here’s few that comes to mind:

  1. Hook gui_input event and check if it is mouse press (event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT).
  2. Add Label as child of Button (setting Button’s styles to StyleBoxEmpty).

It runs, thank you so much for your help :smiley:

Mavitaka | 2023-03-16 19:48