how to connect a button to a different scene?

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

Basically, there are 3 characters that you have to choose from in the character creation scene. To choose one, you click on the texturebutton (no texture), which takes you to another scene where you change the name and class (class is not yet implemented). But I would also like the button to emit a signal to the game scene so that it displays the character that you chose.

:bust_in_silhouette: Reply From: scrubswithnosleeves

You’ll probably need to use an autoloaded scene / singleton. This is a scene that is essentially “global” in that is loaded when the game is first run and doesn’t restart when switching scenes.

The docs explain it pretty well:

To do this, make a new script by clicking “script” > 'File" > “New Script…” in the editor. I name mine “Globals” usually. Then click on “Project” > “Project Settings” and click on the “AutoLoad” tab. Then, click the little file icon and select the “Globals” script you just made, and then click “add”. Now you can access this script from any other scene/script just by typing the name of the script.

For your purpose, I would go into the global script and make a new var class = null. Then in your first scene, when the player chooses their class, you do Globals.class = self.class. Finally, in the next scene, you would do onready var class = Globals.class and then you could set_text(class) on a label to display the class.