Is it possible to get the value of a variable from inside a function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Idleman

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.

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, _physics_process or the _process function, then the value of the variable will be updated. No sure why, but that works for me.

Idleman | 2020-06-25 06:34

:bust_in_silhouette: Reply From: albinaask

Hi,

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 “global_game_states.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.

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.

Idleman | 2020-06-24 02:07

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 add_some_text_to_the_line():
    [the_singelton].example_var = [the_singelton].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():
    print([the_singelton].example_var)

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 “add_some_text_to_the_line” function instead write something like this:

func add_some_text_to_the_line():
        var my_local_var = [the_singelton].example_var + " that is now even longer!!!"

and you’d get the following printout:

My very fine string of text

albinaask | 2020-06-25 16:35

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].example_var = [thesingelton].example_var + " 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.

Idleman | 2020-07-05 05:11

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

albinaask | 2020-07-05 14:37

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

Idleman | 2020-07-05 23:19