0 votes

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 getparent().getnode() 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 SCRIPTA, I write get_parent().get_node("Node_B") and put a .decimal at the end, I am suggested variables from SCRIPTB.

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! :)

Godot version 3.4.2.stable
in Engine by (24 points)

1 Answer

+1 vote
Best answer

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

by (5,128 points)
selected by

Making the variables "onready" worked, thank you!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.