0 votes

Hello,
I dont know why func onlabel_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)
in Engine by (22 points)

1 Answer

+1 vote
Best answer

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).

by (1,650 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.