Using scripting to access a variable from another Node's GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Corruptinator
:warning: Old Version Published before Godot 3 was released.

Hello! I am currently working on another game project and I like to ask if there is any other way to access an another Node’s GDScript to access the variable or property?

For example, I am working on an elevator, there are two objects, one is the Area2D Object with GDScript that allows the object to move out of the elevator and a Switch Object with GDScript as well that activates an animation and triggers a Variable boolean function to tell the Area2D not to let the player out until the animation is done.

Now this is where the “Switch” GDScript needs to access the Area2D’s GDScript, where it can access the Variable to tell the player when to exit the elevator or not.

Is there any way whatsoever to let an object tap into another object’s variable script?

There are many way to do this, but the question for me is: what is the most manageable way to do this.
I don’t like to have many “get_node” calls with paths who can be broken during developpement time. So I’m testing an autoload script who handle node paths automatically.
For the moment my script doesn’t work. My signals connects functions fails because the first entered nodes try to connect to nodes who aren’t entered yet.
I need to catch a signal who warn the script that the scene tree is complete.

DriNeo | 2016-03-21 10:00

:bust_in_silhouette: Reply From: PixelWizzard

There are several ways to access a variable from another node via GDscript. Probably the most elegant way is via setters and getters (functions that return or change a variable inside a node and can be called from another node). There is a simple for this over here. Alternatively you can use get_node("yourNode").get("yourVariable"), which is not as elegant but works as well.

I’ve tried that, but all I got was this:

Invalid type in function 'get' in base 'Nil'. Cannot convert argument 1 from Nil to String.

Corruptinator | 2016-03-21 16:19

That sounds as if you’re either trying to access a variable that doesn’t exist or haven’t specified a variable which value you want to get.

PixelWizzard | 2016-03-21 17:09

broken link

Docs » Engine features » 2D » Custom drawing in 2D
will have a stable link to the 2.1 documentation @
Custom drawing in 2D — Godot Engine (2.1) documentation in English


broken link note :
Godot Docs – 4.1 branch — Godot Engine (stable) documentation in English/latest/…/some_tutorial”
is a moving target when you use ^ /latest/ or /stable/
instead of /2.1/ or /3.x/ which should remain constant

currently this documentation doesn’t exist in the latest branch (yet)

Grass H0PEr | 2017-09-29 23:56

heyy, thanks, it worked ,
i’m trying to access some variable from 1 function, so its helpfull, make the code shorter without have to write all variable or write more function

ruruarchy | 2020-06-01 04:05

this even works but, you need to set as ‘export’ the variable you re going to use in another script.

** _variable's origin **
export [type] _variable;

** other script ** 

__niggue | 2022-02-17 00:01

:bust_in_silhouette: Reply From: Corruptinator

OK! I’ve found a better alternative, turns out that after you call a node, you simply access the variable like this:

get_node("object here").variable

Like that.

func the same too

Kevin | 2017-01-09 06:58