How to add elements of an array as keys and values in a dictionary?

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

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?

Do i understand correctly:

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

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

whiteshampoo | 2020-05-14 07:25

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.

Macryc | 2020-05-14 07:32

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.

Macryc | 2020-05-14 07:42

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.

whiteshampoo | 2020-05-14 07:45

:bust_in_silhouette: Reply From: RazorSh4rk

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?