0 votes

Hi All.
I have an array of:
item1, item2, item3. These are all integers.
I want to write a function that will add these to a dictionary as values to keys. The keys would also need to be added to the dictionary and would need to be the same name as the variables in the array.

So, let's say var array = [item1, item2, item3].
Then the desired dictionary's structure would be:
"item1" = int value of item1 from array
"item2" = int value of item2 from array
"item3" = int value of item3 from array
if that makes sense...
Does anyone have any tips?

asked May 14 in Engine by Macryc (289 points)

Do i understand correctly:

you want the name of the variable (in gdscript) as the key?

var itemxy : int = 42
...
...
...
mydict["itemxy"] == 42

not quite.

more like:
var itemxy = value (this is a singleton var and the value keeps changing during the game)
...
...
...
mydict["itemxy"] = current value (int) of itemxy

I also want it to be that for each var in the array of values it creates a dictionary key that has the name of that value (this is easy) and to that key it assigns the current value of the same-named var in the array.

something like:

for i in array_of_values:
    mydict["i"] = current value of i

the question is, from the array of variables, how do I get each variable have a dictionary key added for itself, with its name as key, and current value assigned as value in the dict.

I think that's not possible.
As far as i know there is no way to get the name of the variable as a string.

1 Answer

0 votes

Atomic variables don't really have names, what you can do is create Nodes from them and add them as dict[object.get_name()] = object.variable

But you are probably doing something wrong here, what are you trying to accomplish?

answered May 14 by RazorSh4rk (256 points)
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.