+1 vote

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?

in Engine by (127 points)
edited by

1 Answer

+3 votes
Best answer

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
by (21,311 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.