How to connect signal from Global autoload to scene node...

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

Im working on making it so that I can teleport a player when a signal is pulled. However, the signal is created in my autoload so that it can be emitted in one scene and connected in another here is the code…

Autoload:

signal left_town

First Scene where its emitted:

func tp_back():
   get_tree().change_scene("res://Scenes/World.tscn")
   Global.emit_signal("left_town")

Scene where its connected:

func _ready():
   Global.connect("left_town", self, "_on_left_town")`

func _on_left_town():
   print("I am never printed."

Right now I am just trying to print something to see if the code works but it isn’t printing the said code… Right now it tells me "The function connect() is returns a value but is never used…

How would I connect the signals? And furthermore would I be able to change the players position with a Player.global_position = Vector2(x,y) in the _on_left_town() function…

Im really new to this and am still trying to learn I’m sorry if its a simple mistake…

1 Like
:bust_in_silhouette: Reply From: Inces

Error You get has no meaning. You connected everything correctly, but your signal is never emitted, because you change whole scene right before trying to emit it. After this line everything is shut down and new scene comes. SO try to just emit signal one libe before you change scene.

I put the emit signal before changing the scene and it still has no effect… my print message isn’t printed…

gummyrat | 2022-01-23 22:01

Ok, I get it now. Still, You change whole scene, so the scene that is connected to the signal is also replaced. Your new scene connects itself in ready, which is after the signal is emitted. Does it print without changescene line ?

Generally I believe changing scenes like this is bad coding practice and is only useful for simplest designs. Anything more complicated, requirring selectively keeping data, will result in a lot of issues when using this method. But perhaps your design is simple. Can You tell something more about how is this teleport supposed to work in game and what scenes are being changed ?

Inces | 2022-01-24 15:30

:bust_in_silhouette: Reply From: ponponyaya

I had a similar question when change scene, following is my solve:

Suppose I change scene from level1 scene to level2 scene. ​

[In Global autoload script]

signal change_scene

var changeSceneFrom = null # only use when change scene


func arrive_new_scene(): # call this function in new scene.
	emit_signal("change_scene")

func clear_changeSceneFrom(): # when things done keep variable null.
	changeSceneFrom = null

[In Old Scene Script]

func change_scene():
	Global.changeSceneFrom = get_name()
	get_tree().change_scene("res://Scenes/Level2.tscn")

[In New Scene Script]

func _ready():

	Global.connect("change_scene", self, "_on_change_scene")

	# The variable "Global.changeSceneFrom" has value when just change scene before, othewise it's null.
	if Global.changeSceneFrom:
		Global.arrive_new_scene()

func _on_change_scene():
	print("Change scene from " + Global.changeSceneFrom + " to ", get_name() + ".")
	Global.clear_changeSceneFrom()

By the way, in my case both of level1 and level2 use the same script which contain all the codes in Old Scene Script and New Scene Script.

Finially, in my test it’s works fine, and I got print like following:
Change scene from Level1 to Level2.

Hope this help.