Accessing Variables from an Autoload Script always return an error

Godot Version

4.3

Question

I’m just doing tests in godot at the moment and so far, trying to access any kind of variable from an autoload has been driving me nuts.

Being a test, everything is very simple.

I’ve created a “Global” autoload, added it to the autoload list (only one so far),
Inside that autoload I have created one single variable titled, well, variable.

My first try was with a bool, so:

“var variable:bool = true”

Then I added another simple scene with a script that only stated:

“if Global.variable:
print(variable)”

Got an error stating: "Invalid access to a property or key “variable” on a base object of type “node(Global.gd)”.

So I tried declaring the variable in my non autoload script instead by doing:

“func _ready() → void:
Global.variable = true”

same error…

Then I thought that perhaps the bool kind did not work, so I turned the variable in an int and just tried to give it a value of 1… same error.

at that point, in desperation, I tried creating a secondary variable of type “Global” and create a new instance of it in my script… and you can guess: same mistake.

At this point I am losing hope that autoloads in Godot are working and of any use considering it is impossible for me to access anything inside of it no matter what I try.

Before giving up entirely tho, I thought of asking for help here.

This sounds correct, if declared on your global script.

The if should work, but the print should fail. Your error states that the if fails though, are you sure you saved the global script after editing it?

You cannot declare a variable for script A inside of script B, declarations must start with var.


It would be easier to follow if you pasted the entire script, here’s how it should be set up

# global.gd
extends Node

var variable: bool = false
# any script
extends Node

func _ready() -> void:
    if Global.variable:
        print(Global.variable)