0 votes

Hey,
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/Character.gd 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
data: Character.gd
extends Node
func _ready():
var Level = 1

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

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

Some idea?

Godot version 3.3.4 stable
in Engine by (30 points)

3 Answers

0 votes

Level needs to be defined as a member variable inside of character.gd:

#Character.gd
extends Node

var Level = 1

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

by (3,888 points)

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

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

Character.gd
extends Node

var Level = 1
var EXP = [40, "required experience for next level"]
var Gold = 0

Label.gd
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?

0 votes

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):
    print(Level)

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

Label.gd

extends Label
var PlayerLevel = Character.Level # should now work
func _ready():
    print(PlayerLevel)
by (6,878 points)
edited by

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?

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

Or

class Character
0 votes

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():
set
text(str(PlayerLevel))

by (30 points)

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

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 [email protected] with your username.