Use variables from other scripts

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

I am trying to pass variables between two scripts without using AutoLoad/Singletons. The scripts are attached to two nodes as illustrated below:

|-o Node_A, [SCRIPT_A]
|-o Node_B, [SCRIPT_B]

When I try to use get_parent().get_node() in one script to access variables from the other script, I am given autofill options that indicate that Godot knows what I am trying to access.

For example, if in SCRIPT_A, I write get_parent().get_node("Node_B") and put a .decimal at the end, I am suggested variables from SCRIPT_B.

Unfortunately, when I write something using these suggestions, like get_parent().get_node("Node_B").var_from_script_b, I get the following compiler error:

- Invalid get index 'var_from_script_b' (on base: 'Nil').

- get_node_or_null: Can't use get_node() with absolute paths from outside the active scene tree.

Again, I am looking for a solution that does not use AutoLoad. Any help I can get would be greatly appreciated! :slight_smile:

:bust_in_silhouette: Reply From: Inces

It seems You just wanted to get your variuables too fast. Wasn’t this line of code before ready() or on init()?. Error makes me think, that one of nodes was not ready at the moment when another node tried to reference its absolute path. If so, just make references ONREADY, and get your variable in ready function

Making the variables “onready” worked, thank you!

358JH33E | 2021-12-25 12:21