Get value of key from dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bobokox
:warning: Old Version Published before Godot 3 was released.

Hey!
i want to read the value of a key from a dictionary using a for loop. is it possible to do this with a variable instead of using the name of the key?

onready var items = get_tree().get_nodes_in_group("item")
var vitem = {}
func _ready():
	set_fixed_process(true)
func _fixed_process(delta):
	for i in items:
		var name = i.get_name()
		vitem = {name:0}
	for i in vitem:
		if vitem.i > 0:#######################
			print(i)
			var pic = get_node("items/"+str(i))
			pic.set_pos(Vector2(0,0))

The problem is in the line with the quotes. i also tried vitem.i.value() and stuff, but it don’t work.

thank you very much!

Scratch first comment.

If you want to access a key via a string variable, you can do something like this:

var dict = {}
dict.a0 = {name = "yours", num = "100"}
dict.a1 = {name = "mine", num = "101"}

for i in range(dict.size()):
	var key = "a" + str(i)
	print(dict[key])

avencherus | 2017-02-09 00:31

thank you.

but if i do it with equals sign its not possible to write a variable as key.

var name = i.get_name()
vitem = {name:0}
print(i)

output is “seed” - which is the name of the object

var name = i.get_name()
vitem = {name = 0}
print(i)

output is “name”.

is there a way to easily write and read it?

bobokox | 2017-02-09 00:48

Sorry, I’m misunderstanding what you’re trying to accomplish.

avencherus | 2017-02-09 00:54

Are you meaning to do something like:

for value in vitem.values(): print(value)

avencherus | 2017-02-09 00:58

sorry, maybe i should explain.

im working on an inventory. i have a group named “item”. the script should write a dictionary with all the objects in the group with a starting value of 0. (first loop)

for i in items:
        var name = i.get_name()
        global.vitem[name] = i.value

the second loop should check if there is an entry in the dictionary with a value > 0. and finally it should place this object in an inventory slot.

Edit:
Just saw your second post. its getting close to what i need. but i totally need the connection between key and value… means that i need to access the value when i have the key or the other way.

bobokox | 2017-02-09 01:02

Well the ways I would loop through it would be one of these two choices:

for value in vitem.values():
	print(value)
	if(value > 0): print("greater than 0")
	
for key in vitem:
	printt(key, vitem[key])
	if(vitem[key] > 0): print(key + " is greater than 0")

avencherus | 2017-02-09 01:14

this is exactly what i wanted!

thank you very much!

bobokox | 2017-02-09 01:19

You’re welcome. I’ll post that as the answer for others then.

avencherus | 2017-02-09 01:36

:bust_in_silhouette: Reply From: avencherus

One of these two methods.

for value in vitem.values():
    print(value)
    if(value > 0): print("greater than 0")

for key in vitem:
    printt(key, vitem[key])
    if(vitem[key] > 0): print(key + " is greater than 0")