Trying to share variables between scripts

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

I try to use for example :

Script1.variable_name

but get error:

Invalid get index 'variable_name' (on base: 'PackedScene').

if I make a new script and try the same thing I get error:

Invalid get index 'variable_name' (on base: 'GDScript').

and if i try this:

var variable_name = get_node('..').variable_name

I get:

Invalid get index 'variable_name' (on base: 'null instance').

So I’m missing something obvious but can’t find any info about it. Any help is appreciated!

1 Like
:bust_in_silhouette: Reply From: wombatstampede

Ok, if the script is attached to a node in the current Tree (i.e. a loaded scene) then you actually can just adress it by adressing the node(dot)variable-name. You once did this in your example but you obviously adressed the wrong node. I can’t tell though why because I have no idea how your scene looks and from where to where you want to adress that node.

You can’t set variables directly in scripts but in instances of scripts.

The easiest way are Singletons which you can declare in the project preferences. This is typically used for global/shared variables:

About adressing variables (or functions) in other nodes:
If you’ve three scripts attached to three nodes in a scene:
Node1:
…Node2: (child of Node1)
…Node3: (also child of Node1)

From Node2 (and Node 3) you can access some variable in Node 1 with
get_node("..").variable_name = 123

From Node1 you can access some variable in Node 2 with
get_node("Node2").variable_name = 123

From Node2 you can access some variable in Node 3 with
get_node("../Node3").variable_name = 123

:bust_in_silhouette: Reply From: bender

I get this error when the path of the variable (and the node) is wrong.
So just to be sure I am working with the right paths, I print their names.
In the following example I use these 2 instructions:

print("parentnode: ", parentnode.get_name())
print("parentvar: ", parentnode.parentvar)

EXAMPLE
I have 2 spatial node, node_parent ad node_child.
And 2 scripts, node_parent.gd ad node_child.gd, attached to them.
Below the content of the scripts.

#start current script: node_parent.gd
#I have a spatial node named node_parent
#node_parent.gd is a script attached to the spatial node node_parent
#This is the content of node_parent.gd
extends Spatial
onready var parentvar = 11224455
#end current script: node_parent.gd

#start current script: node_child.gd
#I have a spatial node named node_child
#node_child.gd is a script attached to the spatial node node_child
#The spatial node node_child is a node child of the spatial node node_parent
#This is the content of node_child.gd
extends Spatial
onready var parentnode=get_node(“…”)
func _ready(): pass

func _process(delta):
print("parentnode: ", parentnode.get_name())
print("parentvar: ", parentnode.parentvar)
#end current script: node_child.gd