+1 vote

I want to change to the next scene by clicking the label.

My code:

extends Label

var current_scene = null

func _ready():
pass

func onStartguiinput(event):
pass
if (event is InputEventMouse && event.pressed && event.labelindex == 1):
if get
tree().change_scene("res://assets/Greetings.tscn") != OK:
print("An unexpected error occured when trying to switch to the Greetings scene")

Godot version 3.2.3
in Engine by (23 points)

Sorry, what specifically is the issue with your code?

Well, i used this code to use the label to change my scene, but it doesn't work when I pressed the label, so I need help on making it work.

3 Answers

0 votes
Best answer

You could assign the left mouse button an action in the InputMap and check for that action or use this code:

if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
by (6,296 points)
selected by

I don't have a mouse, so what should I do?

Wait, you don't have a mouse? Then how do you click on the label?

With a track pad. But even when I get a mouse, I still couldn't change the scene when pressing the label.

Okay, when I say mouse, I don't mean the actual device - I just mean anything you can click with. It should work with a trackpad.

Alright. Though, I found out that I can change scenes by using the timer. Still, thanks for helping me.

0 votes

really simple.

just get an button and set it over your label.
then make the button invisible like so.(attach script to it)

func _ready():
self.visible = false
pass

then connect the button on "button down" with the Spatialnode ,attach again first a script to it,and write in the new func:

gettree().changescene("res://path/to/scene.tscn")

ago by (137 points)

the second time, attach the script to the Spatial

0 votes

As another option if you want to use a Label only, you can check if the mouse click is inside the Label area as follows:

func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and event.button_index ==1 and event.is_pressed():
        var mouse:Vector2=get_global_mouse_position()
        var label: Label=get_tree().root.get_node("Node2D/MyLabel")
        var rect:Rect2=Rect2(label.rect_position, label.rect_size * 2) 
        if rect.has_point(mouse):
            print("Clicked Label. Change Scene here")
            pass
ago by (602 points)
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 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 webmaster@godotengine.org with your username.