How to link a var to a node in a different scene ?

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

How to link a variable to a different link ?

Can you elaborate a little more what do you mean with “link”?

eons | 2016-10-17 02:40

too short question to answer

volzhs | 2016-10-17 15:31

link like
var label = get_node(“label”) or get_parent().get_node
get it?

dezekeerjoran | 2016-10-17 16:01

:bust_in_silhouette: Reply From: eons

If you want the text of a child label called “label”:

var label_text = get_node("label").get_text() 

If the variable you are looking for does not have a default getter, you can make it in it’s script, like:

func get_variable():
    return variable

then do

var label_variable = get_node("label").get_variable() 

Everything is in the docs, here: http://docs.godotengine.org/en/latest/reference/gdscript.html

If you want to pass variables around scenes, like using globals, you can store them in singletons (search for autoload)

Do not wanna be annoying, but that’s not really what. I mean its like:

var something = get_node(“something”)

but that’s in the same scene i want it to get a node in a different scene.
thanks that you wanna help me not like volzhs ho just says its a bad question.

dezekeerjoran | 2016-10-18 15:52

You have to admit that the question is a bit vague :slight_smile:
But I think it is taking shape…

If you have many scenes and want to access to a node that is not on the current active tree, you need to load and instance the scene in a variable, then, that variable will be the root node of that scene.
Over that instance you can work before (or without) adding it to the tree.

Instancing won’t trigger the _ready, only the _init.

More on instancing:
http://docs.godotengine.org/en/latest/tutorials/step_by_step/scripting_continued.html#instancing-scenes

eons | 2016-10-18 16:27

does it has to be under func _ready because it says unexpected token

dezekeerjoran | 2016-10-18 17:46

That is a syntax error somewhere in your code

eons | 2016-10-18 18:01

alread but how do you use it because it says indentifier not found: …

dezekeerjoran | 2016-10-18 18:07

i will just say where i am working on maybe it will be clearer.
i want to create a TextEdit where you write something in an it will appear in a Label but in a different scene.
So I have to write:

var scene = preload(“…”)

func _ready():
var Label = scene.instance()
add_child(Label)

func _on_TextEdit_text_changed():
var edit = get_node(“TextEdit”)
Label.set_text(edit.get_text())

but then it says:
Nonexitent function ‘set_text’ in base ‘GDNativeClass’

dezekeerjoran | 2016-10-18 18:20

Now we are going somewhere…

That Label is on _ready function scope, you want it outside (an instance variable).
Also, it points to the root node of the preloaded scene, if it does not have a function get_text, will throw an error.


Now depends on what are you doing, if you plan to add to the tree the instance Label, you will get the text (unless a _ready in Label does something to it).

If you change scenes to get the text on a new main scene, you may lose reference to Label, the way to solve that could be a singleton used to store the text (like passing a score from game to menu).

Tutorial for singletons is here:
http://docs.godotengine.org/en/stable/tutorials/step_by_step/singletons_autoload.html

ps: You may need to follow some tutorials based on GDScript programming (or maybe python, is similar) because looks like you are not familiarized with basics like variable scopes.

eons | 2016-10-18 23:01

Now have i autoload a script and the scene ,but I don’t get what that has to do with variables. And do I have to autoload the scene where you go to.

dezekeerjoran | 2016-10-21 21:07