Calling functions from one scene to another

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

How would I go about calling a function from a different scene? I have a level and I want to change the scene to the victory_achieved scene once my player reaches the target. But I’m lazy so instead of making a victory_achieved scene for every level I want to use the same one but change the text of the labels to read something else. In this case I make a function for this text changing event then I put the text as one of the function arguments. This is all well and good but I need to call it from the level script and that is where I am stuck. How would I call that function, then change the scene but keep the text as what was stored in the arguement?

:bust_in_silhouette: Reply From: Gluon

You need an autoload script. Basically you can just create a node with a script attached and add a variable to hold a string. Then go into project > project settings. In the tabs you will see autoload. Go to the autoload screen and you can add any node to this, what this will do is automatically load the scene you add whenever you load any other scene, so it is the equivalent of adding it to each scene you load in the game.

What you can then do is change the variable in the autoload script to hold the text you want and then on your victory screen just look at the text in the autoload script and use that to assign to the label.

I was basically going to suggest the same thing. Autoloads are perfect for managing scene transitions instead of doing that in each script.

magicalogic | 2022-12-29 16:01

My level complete screen is a spatial node with textures so when I autoload it appears in every scene as visible. I add a hide method to the ready function?

Genesis689 | 2023-01-02 13:31

You do not make that an autoload, you create an autoload to create the level complete scene and show it, as well as free it when it’s no longer required.

magicalogic | 2023-01-02 16:03