(Answered) How to make persistent variables?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PugMasterPug
:warning: Old Version Published before Godot 3 was released.

I am creating a game, which has levels, and to play a level you must complete the previous one, the problem is that each level is in a different scene, so I want to create a variable which can persist even if the scene is changed, the variable would tell what level is the most advanced level you completed, and when you complete the next level, that variable increments by 1

How could I do it?

:bust_in_silhouette: Reply From: kidscancode

For, this you want to use a Singleton. In Godot, you do this by making a script, let’s call it “global.gd”. For example:

extends Node

var level = 1

Now, go into Project Settings → Autoload, and add this script, giving it a node name (for example “GameState”). Don’t forget to check “Enable”.

Now from any script in the project, you can access vars and methods from this script like so:

GameState.level += 1