Using global variables autoload,

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fyrion

I tried to work with autoload global variables but it is not working quite yet.

  1. Added a script, saved and implemented it into autoload res://Variables/ and defined an variable var Level = 1 inside.

  2. Created a interface control with a label, which text should be updated by the current player level defined in the global variable.

Global variable script
extends Node
func _ready():
var Level = 1

Label script
extends Label
var PlayerLevel = Character.Level
func _ready():

Error when starting the scene: Invalid get index ‘Level’ (on base: ‘Node (’].
Can’t find any solution - already tried to use arrays but parsing error by debugger is the same.

Some idea?

:bust_in_silhouette: Reply From: timothybrentwood

Level needs to be defined as a member variable inside of
extends Node

var Level = 1

With the way you have it defined Level is only accessible during and from the _ready() function inside of

Hey mate, thanks for your suggestion. My idea behind was that the global function is available with the loading of

That means basically I define many variables just without any function in a script to provide these with autoload for the whole project?
extends Node

var Level = 1
var EXP = [40, “required experience for next level”]
var Gold = 0
extends Label
var Level = Character.Level
var EXP = Character.EXP
var Gold = Character.Gold

func _ready()
print(Level + EXP[0] + EXP[1])
print (Gold)

This way should be display Level and required experience and current gold currency?

Fyrion | 2021-12-02 05:24

:bust_in_silhouette: Reply From: Wakatta

Local variable

The following variable is actually local because you can access it only in the block or function in which it is declared.

func _ready():
    var Level = 1

Global variable

A Global variable exists outside of a function block like the following and can be accessed by any function of this class object

extends Node

var Level

func _ready():
    Level = 1

func _input(_event):

Class Reference

Normally one would have to specify a class’s path to get it’s reference and by making it and autoload you essentially make it a Global class to be accessed throughout the entire project

extends Label
var PlayerLevel = Character.Level # should now work
func _ready():

I just mind asking: Is it neccessary as single standing script with no extension using the line “extends Node” as there is no node that references to it?

I saw that these line is not inside your first global variable script.

If it is like I assume, I understand the error way better as Godot searching a node with global references which does not exists?

Fyrion | 2021-12-02 07:02

No that was my bad a typo if you will, repaired

It must extend a Node as it will be added to the sceneTree

So the following will not work

extends Reference


class Character

Wakatta | 2021-12-02 13:51

:bust_in_silhouette: Reply From: Fyrion

For everybody: I found the solution with the help of those both guys.
I tried to use a int from a global variable in a label which only displays strings.

extends Label #First label displaying Level:
var PlayerLevel = GlobalVariables.PlayerLevel
func _ready():
set_text("Level: ")

extends Label #Displaying current level on base of global variable
var PlayerLevel = GlobalVariables.PlayerLevel
func _ready():

Oh yeah. Good work. Had not picked up on that

Wakatta | 2021-12-02 13:54