problem with change scene

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

Hi, i want to run a specific line of code after the scene was changed.
I changed the scene with (get_tree.change_scene()).
Is there a specific signal for this or something else?
Thanks in advice.

Why don’t you add the code you want to run in the main script of the scene that you are switching to.

Scavex | 2021-02-01 12:43

Ok that’s it.
After the scene was changed:

position = Vector2(0,100)

In to the player script.

Simon | 2021-02-01 12:46

See if it works for you. The script where you are using get_tree.change_scene(), try adding onready var playerReference = get_parent().get_node("Player") and then use playerReference.position = Vector2(0,100)
I am assuming you have named your player Node as Player and it is part of the main Scene.

Scavex | 2021-02-01 12:55

Thanks i’ll try it. But my problem is that this line of code must run after the change scene into the new scene . I used a node 2d with a script to interconnect this two scenes. My real problem is how can i emit a signal after a scene was changed.

Simon | 2021-02-01 13:32

Have you tried using Signals? Can you share your project ? Maybe we can find a solution.

Scavex | 2021-02-01 13:44

Yes i use signals but my problem is this:
“How can i emit a signal AFTER the scene was changed?”

Simon | 2021-02-01 13:46

I guess by using a custom signal in the _ready() function. You first declare a signal as

signal example

then you use emit_signal("example") in _ready() function. Then you select the node with which the script containing example signal is attached and go to the Inspector tab where you will see a example signal under Signals that you created. Connect that and you’re good to go. However you must have a script in the scene that you are switching to. There are definitely people here in the community with amazing knowledge but I don’t know why they haven’t answered your question yet. Let’s hope they’ll resolve your query but make sure you try my fix too.

Scavex | 2021-02-01 15:15

I do exactly like that but it doesn’t work. I connect the signal to the main script but it doesn’t work.

Simon | 2021-02-01 15:21

:bust_in_silhouette: Reply From: lofi

do you haveposition = Vector2(0,100) inside a player’s response to signal function?