How to use label, and only label, to change to next scene by clicking it?

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

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

My code:

extends Label

var current_scene = null

func _ready():
pass

func _on_Start_gui_input(event):
pass
if (event is InputEventMouse && event.pressed && event.label_index == 1):
if get_tree().change_scene(“res://assets/Greetings.tscn”) != OK:
print(“An unexpected error occured when trying to switch to the Greetings scene”)

Sorry, what specifically is the issue with your code?

exuin | 2021-03-06 18:57

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.

Geox2 | 2021-03-06 19:01

:bust_in_silhouette: Reply From: exuin

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:

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

Geox2 | 2021-03-06 19:17

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

exuin | 2021-03-06 19:27

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

Geox2 | 2021-03-06 19:30

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.

exuin | 2021-03-06 19:35

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

Geox2 | 2021-03-06 19:56

:bust_in_silhouette: Reply From: theMX89

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:

get_tree().change_scene(“res://path/to/scene.tscn”)

the second time, attach the script to the Spatial

theMX89 | 2021-06-19 06:27

:bust_in_silhouette: Reply From: wyattb

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