Switch scenes with parameters

Godot Version

4.2.2.stable

Question

Hello everyone!

Im fresh to Godot and trying around some stuff.

My question is: How can i pass variables around scenes?

What i want to archive is:

In my CharacterSelectScene choose an character then pass the selected character data (as Dictionary) to the GameScene where i can process it in the _ready() where i got my WebSocket setup and need to pass the characterId as an header.

Ive searched for some solutions and found some stuff but it dont feels right.
For example a Singleton script isnt what i want.

The things Ive tryed failed because the GameScene wasnt ready yet.

Thanks :smile:

Hmmm, in the Godot project folder manager, create a script, “Global.gd”, and add the codes what you want to share in all scenes, then go to project settings>Auto loads, there you will find a add button, add the global script, now to use this in any script just use it like this print(Global.my_var), start with Global.

Yes, the simplest solution is to set up an autoload script that would work as a holder of all variables you need to share across multiple scenes. You set them in one scene, change the scene and read them in the second scene.

@KingGD @FencerDevLog Thank you both for your replies.

I know the “AutoLoad” would be an easy solution:

For example a Singleton script isnt what i want.

The reason I dont think its good is because, initially its going to be an easy and fast solution, but in the long run you shoot yourself in the foot for handling global variables.

I got an GlobalVariables script, but there I only house const values that not going to change in the runtime.

What Im searching is more like a “constructor”.

Thank you!

1 Like