How to assign an autoloaded variable to an exported variable?

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hey there! I’m trying to write a script that checks if the player has done a certain thing (by checking whether or not an autoloaded variable, called say GlobalVariable.DefeatedBoss, is set to true), and I would like to be able to reuse the script in multiple scenes, and in turn, check different autoloaded variables (such as GlobalVariable.SavedNPC or GlobalVariable.FoundLoot) depending on what the scene needs. I thought I’d do this using something like this in the script:

@export var checkedVariable: bool
...
...
if checkedVariable:
#stuff happens

And then assign the desired autoloaded variable in the inspector like you would with other exported variables when I make different scenes. This didn’t work of course, as what shows up in the inspector is just a check box indicating whether or not the variable should be set to true or false. I tried replacing “bool” with other stuff, but couldn’t think of what to use. The closest I got is this:

@export var checkedVariable: GlobalVariable

Which asked me to assign a node in the current scene for some reason.

Is it possible to do what I’m trying to do? If so, what should I put instead of “bool” to make this work? And if not, how can I make a single script check for different variables depending on the scene’s needs? Thanks in advance!

Why doesn’t this work for your autoload scene?

Do you mean why I can’t enter the autoloaded variable I want? Because when I use @export checkedVariable: bool, instead of asking me to enter a string with the variable’s name or something in the inspector, all it shows there is a checkbox like this:

Or if I use @export checkedVariable: GlobalVariable, it asks me to choose a node, not a variable. The script I’m writing is for a scene that’s separate from the autoloaded scene/script where the GlobalVariables are stored.

Ah maybe you do need your export to be a string, then use the .get() method on your autoload?

@export var checkedVariable: String

func _ready() -> void:
    var checked_value = GlobalVariable.get(checkedVariable)

That worked perfectly, thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.