0 votes

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.globalposition = Vector2(x,y) in the _onleft_town() function...

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

in Engine by (25 points)

2 Answers

0 votes

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.

by (5,659 points)

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

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 ?

0 votes

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.

by (526 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.