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?

in Engine by (506 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?

by (267 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 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.