• Godot HelpProgramming
  • What is the correct way for common variables needed to be access in multiple scenes/scripts?

Hi, I tried

myvars.gd
	var min_height = 10
	var max_height = 20

scene1.tscn
	const myvars = preload("myvars.gd")
	var box_height = myvars.min_height

That gave me an error of Invalid get index 'min_height'

@duane said: When I need "global" variables, I put them in a singleton (usually called G.gd) that autoloads. See the tutorial for the full explanation:

https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html

Thanks, this could work. It won't allow this though

scene1.tscn
	var myvars = get_node("/root/myvars")
	var box_height = myvars.min_height

get_node has to be call inside onready and that means box_height's value cannot be set during declaration.

Maybe I'm misunderstanding you. Can't you put myvars.gd in as an autoload and use any value from it at any time? It should be loaded first.

If you need box_height to be global, just assign it to a variable in myvars once it's available. Is there some case where you need the result before onready?

@duane said: Maybe I'm misunderstanding you. Can't you put myvars.gd in as an autoload and use any value from it at any time? It should be loaded first.

If you need box_height to be global, just assign it to a variable in myvars once it's available. Is there some case where you need the result before onready?

Yes I can and it works. I just need to re-arrange my code because originally the values were assigned at top of the script where the variables are declared.

That won't work because get_node can't be called at variable declaration it needs to be called inside onready.

@newguy said: Yes I can and it works. I just need to re-arrange my code because originally the values were assigned at top of the script where the variables are declared.

That won't work because get_node can't be called at variable declaration it needs to be called inside onready.

Dont know if you mean the _ready function or if you know about onready so just in case you dont know:

onready var myVar = get_node(node) works outside _ready()

@7892 said:

@newguy said: Yes I can and it works. I just need to re-arrange my code because originally the values were assigned at top of the script where the variables are declared.

That won't work because get_node can't be called at variable declaration it needs to be called inside onready.

Dont know if you mean the _ready function or if you know about onready so just in case you dont know:

onready var myVar = get_node(node) works outside _ready()

Yes but there is a problem,

onready var myvars = get_node("/root/myvars") var box_height = myvars.min_height

myvars.min_height value is null

See if this page helps. https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html

Also, you can't just run code wherever you want. It typically should be in the _ready() function if you want to initialize or modify values. The variables at the top of the script should just be declarations. However, it might work if you do this:

onready var box_height = myvars.min_height

@cybereality said: See if this page helps. https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html

Also, you can't just run code wherever you want. It typically should be in the _ready() function if you want to initialize or modify values. The variables at the top of the script should just be declarations. However, it might work if you do this:

onready var box_height = myvars.min_height

Yes that works too but I think you are right I should just initialize all my variable values inside _ready(). Thanks.

9 months later