Is it possible to instansiate global variables?

Godot Version

4.2.1

Hello,

I have two global scripts (added to project settings/autoload) that i use to manage comunication between nodes (Global) and tracking story progression (Story). Is it possible to instansiate these scripts so that i can save and load them using:

    var packed_scene = load("res://Saves/SavedGame.tscn")
	# Instance the scene
	var level_instance = packed_scene.instantiate()
	main2D.add_child(level_instance)

I currently have the entire game instansiated to a node call MainMap, which is what i save into the tscn file.

Thanks in advance!

EDIT: I realised this is probably not possible to do, I can however instantiate a local script and use the global scripts as a reference holder, which is what i will be going for!

If you have an autoloaded script, you can define define variables in that script and access them anywhere in your project. For example, if your autoloaded class is called Story and it has a variable called foo, you should be able to access it using:

Story.foo

anywhere in your project.

Your question and code imply saving progress on a game. Are you trying to save progress in a game? That would be different.

Yes, that is what i am trying to do.

I have a dict in the global script Story which i update according to certain story events:

var mainStory: Dictionary = {
	#Dropoff events
	"left_dropoff" : 0,
	"talked_to_silas_at_dropoff" : 0,
	"talked_to_jeb_at_dropoff" : 0,
	"killed_silas_at_dropoff" : 0,
	"jeb_died_at_dropoff" : 0,
	"stalkedTheBrave_died_at_dropoff" : 0,
	"talked_to_holly_at_dropoff" : 0,
	
	# Bogtown surrvive events
	"arrived_at_survive" : 0,
	"talked_to_jeb_at_survive": 0,
	"survive_complete": 0,
	}

Ahh, then take a look at the docs for Saving Games and keep in mind that there is a special path user:// that needs to be used when writing persistent files. You’re on the right track with the dictionary. The tutorial shows you how to turn it into a JSON file and read it again in the future.

1 Like

I will try to use packed resources until i run into a problem, much less work! Good to know where to look when it all breaks down :smiley:

You don’t instantiate globals, as that is contrary to the entire purpose of globals.