Topic was automatically imported from the old Question2Answer platform.
Asked By
JasperC
This has been asked a lot, but none of the answers made sense to me. Basically, I have a variable in one script that is constantly being updated, and I need to export it to a different script to use it.
I believe I’m supposed to use get_node() to access the other script and then use dot notation like nodewiththescript.variableiwanttoexport somehow? I don’t get it.
How do I use get_node, is it just standing alone or is a variable assigned to it or what?
How do I use dot notation and the node.variable thing?
Node A wants to access a variable inside the script attached to Node B. Inside the script attached to Node A you would need to define a variable with a reference to Node B as such:
onready var node_b = get_node("path/to/NodeB") # The path in the tree, not the file system.
Once this is done you can access
node_b.variableiwanttoexport
in any way you want.
However, it’s important to note that this is only correct if NodeB is a child of NodeA. If they are siblings or are very far away in the tree it’s better to use signals to limit coupling and have a cleaner architecture. This would avoid problems in the future in case you changed the structure.
In that case you would emit a signal from NodeB indicating that the variable has been modified and pick it up in NodeA to get the updated value.
I really recommend you take a look at this guide, it’s been very helpful to me: