–2 votes

How to link a variable to a different link ?

in Engine by (54 points)

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

too short question to answer

link like
var label = getnode("label") or getparent().get_node
get it?

1 Answer

+2 votes

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)

by (7,868 points)

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.

You have to admit that the question is a bit vague ^_^
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:

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

That is a syntax error somewhere in your code

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

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()

func onTextEdittextchanged():
var edit = getnode("TextEdit")

but then it says:
Nonexitent function 'set_text' in base 'GDNativeClass'

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:

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.

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.

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.