0 votes

If there is a variable outside of a function with an initial value, then inside the function the value changes. I tried to get the variable from another node with get_node, but It only gets me the initial value that is out side of the function not the changed one inside. Is that a way to get the changed value of the variable from inside that function? Thank you.

in Engine by (266 points)

After some more work, I have found out how to make it work. If I get node as a member variable, it only gets me the initial value. But if I do it inside ready, _physicsprocess or the _process function, then the value of the variable will be updated. No sure why, but that works for me.

1 Answer

+1 vote
Best answer


GDScript is an object oriented language, that means that all the non-function variables (also called properties) their data are stored on the object, meaning that every instance (or node in this case) have their own unique data attached to their variables, meaning that two nodes with the same script attached can have entirely different values of their variables.

That is why you are getting the different values, so what you will have to do is to store the data that you want to be shared in the same place, which can be done in different ways, the easiest in my opinion is singeltons, which you can read about in the docs, just creating a "globalgamestates.gd" will suffice for a small project (aka a project with less than ~100 different scripts) but can become messy in a larger context, in which one has to be a little more creative. I think however I will leave you there for now since there is no fit-all solution for bigger projects and each problem has to be solved by a kind of case to case basis which most commonly (in my case) finding a common grandparent somewhere in the tree and storing them there.

by (212 points)
selected by

Thanks for providing these information. I have looked to the Singletons way of storing value in a global variable. I understand the concept a bit clearer now, but still have quite figure out that if defining a variable in the global script and then the variable got updated in a local function, how do the local function pass the updated value back to global variable so that other nodes could access it? Thank you.

basically all variables(except perhaps floats, ints, and bools, not sure) are passed by reference, this means that they may have many references, but only a single value, for example:

singelton code:

var example_var := "My very fine string of text" #storing a "global" string, able to be accessed anywhere! The coma only ensures that only strings may be stored in this var

somewhere in the code inside a node:

func addsometexttotheline():
singelton].examplevar = [thesingelton].example_var + " that is now even longer!!!"

this will update the string globally, if you then access it somewhere completely different later:

func some_later_func():

you will get the following printout:

My very fine string of text that is now even longer!!!

if you want to avoid this behaviour, you'd in your "addsometexttotheline" function instead write something like this:

func add
var mylocalvar = [thesingelton].examplevar + " that is now even longer!!!"

and you'd get the following printout:

My very fine string of text

Thank you for this detailed explanation. I have a better understanding about this now. Just a quick question to confirm something, in this code:

func addsometexttotheline():

[thesingelton].examplevar = [thesingelton].example_var + " that is now even longer!!!"

should it be
[thesingelton].examplevar = [thesingelton].examplevar + " that is now even longer!!!"

if so then I understand, but if it is [thesingelton].examplevar, then I don't quite understand the use of examplevar in this example.

Yes, defiantly!! You are completely correct that I misspelled examplevar to example_var, easily done outside the code editor :)

Thank you for clarify that for me, now I know how this variable works.

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 Frequently asked questions and 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 [email protected] with your username.