how can i get variables from other scripts?

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

please i already tried everything

can use get_node("Child").test = 0 or use $ as shortcut $Child.test = 0

rakkarage | 2020-08-06 03:28

Make a new node/scene Globals or any name you want to give it . Then create a script and there declare variables . Then go to Project Settings and in Autoload Tab ,make the Globals a Singleton. Now you can use it anywhere you want by -

Say for example , var data is there in Globals and Globals is Singleton,
Then ,in any other script , you can write :-

Globals.data , which will let you access it.

Please visit Singletons (Autoload) — Godot Engine (stable) documentation in English for more information.

Otherwise ,if the script is of the child node of one single node ,then just out “$” and then write the node script you want (provided it is under the same parent node).

sarthakroy | 2020-08-06 03:46

:bust_in_silhouette: Reply From: sarthakroy

Make a new node/scene Globals or any name you want to give it . Then create a script and there declare variables . Then go to Project Settings and in Autoload Tab ,make the Globals a Singleton. Now you can use it anywhere you want by -

Say for example , var data is there in Globals and Globals is Singleton,
Then ,in any other script , you can write :-

Globals.data , which will let you access it.

Please visit Singletons (Autoload) — Godot Engine (stable) documentation in English for more information.

Otherwise ,if the script is of the child node of one single node ,then just out “$” and then write the node script you want (provided it is under the same parent node).

:bust_in_silhouette: Reply From: Liemaeu

I use a global script for this (as far as I know this is the best working and “cleanest” way).
Create a script (right click in the file browser → New script) and make it gobal:
Project → Project settings → Autoload → click on the “folder”-icon next to Path, select the created script and click on Add. It is now shown in the list below.

In the global script you can write get- and set-functions, e.g.:

var variable

func set_variable(var v):
    variable = v

func get_variable():
    return variable

You can call the set_variable(v) and get_variable() in your other scripts, with Global.set_variable(v) and Global.get_variable() (replace “Global” with the name of the global script, shown in the name column in the autoload project settings list).