Dinamically calling parent variable for a extension

I have read the previous topics on calling variables, but they acually dont solve the issue. I know it is possible to call a child variable, but there seems to be no solution about a parent one.

How do I…

How do I call a parent variable. Not a child one. If the feature does not exist, how do I ask it to be added, because I think it could be super usefull.

What is the expected result

For the sake of an extension, the most user-friendly experience would be that the user picks from a list of predefined values (eg. red, green, blue) that correlate with some variable. However, unless the variable is a child (eg. Hat.red, Hat.green, Hat.blue) it is impossible to call it.

What is the actual result

If I have a variable string “style” that stores the desired values (eg. red, green, blue, as child variables from Hat) I can later call for Hat[style]: and it gives me whatever value is stored in Hat.red, Hat.green or Hat.blue, depending on the string value. That is good.

But, if I have a variable string “stylish” that stores the desired values (eg. Red, Green, Blue as parent variables without childs) I cannot later call for [stylish] and obtain whatever value is stored in red, green or blue, depending on the string value. Of course I could call directly for the variables instead… but I dont know which variable is going to be choosen.

So instead of one command (Change the variable X set to [stylish])
I have to use three conditions
If stylish=“red”, then change the variable X set to Red.
If stylish=“blue”, then change the variable X set to Blue.
If stylish=“green”, then change the variable X set to Green.

Now imagine if, instead of three, there are 10, a hundered on an indefinite number of options? It is not that the problem does not have a work around, but

  1. I want to know if I am missing or missunderstanding something.
  2. Being able to call for variables, including parents, would be an improvement and, hopefully help scalability and performance in extensions.

Top level variable names are references rather than strings, and cannot be called dynamically.

The closest workaround would be to make a too level variable names like, GameData, and make all of your other variables children of it. That way you can always call them as child variables.

1 Like

Creating a single structure like Data that holds everything can simplify things like saving/loading by being able to load/save just 1 variable.

If it helps, you could pass the scene or global variable to the function as type variable. You could then copy the variable to an extension variable and use the value of another parameter to select children variables.

https://forum.gdevelop.io/t/not-a-bug-variable-parameter/65018/2?u=keith_1357

You could pass the variable and the child’s name to the function and the function could do something or if it’s an expression, it could return a value. Or it could modify the extension variable and then copy that variable back to the variable selected by the parameter variable.

1 Like