How to loop through a specific set of keys in a dictionary godot

im using godot version 4.7.2 stable

im trying to make it so i can loop through a specific set of keys in a dictionary, but i cant find anything that works, i’ve tried a few methods but none of them worked

What have you tried? Generally you have to filter out what you need with if statements, what does your data look like? What do you want filtered?

i’ve tried doing something like this, tweaking a few keywords each time but it didnt work

var blocks_scene_table = {
	"baby_ant" : {"path" : preload(Constants.ENEMY_SCENE_PATHS.baby_ant),
				"weight" : 50000},
	"worker_ant" : {"path" : preload(Constants.ENEMY_SCENE_PATHS.worker_ant),
				"weight" : 30000},
	"angry_ant" : {"path" : preload(Constants.ENEMY_SCENE_PATHS.angry_ant),
				"weight" : 15000},
	"scientist_ant" : {"path" : preload(Constants.ENEMY_SCENE_PATHS.scientist_ant),
				"weight" : 10000},
	}


func _random_choose():
	var keys = blocks_scene_table.keys()
	var weight_sum = 0
	#loops thru all enemies in the table to then add their weight for a total sum.
	for enemy in range(0,StatsHolder.max_enemy_tier):
		weight_sum += blocks_scene_table[enemy]["weight"]

(max enemy tier is 2 btw)

Your loop iterator enemy is an integer. Then you try to use that integer as a key for your dictionary, which won’t work since there are no integer keys in that dictionary.

What was your intent with looping from zero to max_enemy_tier?
Perhaps blocks_scene_table[keys[enemy]]["weight"]

In general, when your loops don’t work, always put some print statements inside the loop body to see what it’s iterating over.

Yeah it’s odd that you get the keys but don’t use them. keys will be an array of strings which can be iterated.

for enemy_key in keys:
    weight_sum += blocks_scene_table[enemy_key]["weight"]

Oddly (and I don’t like this as language syntax) this is how dictionaries iterate with for loops anyways, keys is not necessary.

for enemy_key in blocks_scene_table:
    weight_sum += blocks_scene_table[enemy_key]["weight"]

I agree with normalized that you’re trying to use integers as keys in a dictionary with only string keys.

Additionally, if you’re trying to only use certain keys in that dictionary (such as based on enemy tier), you could keep those keys in a separate array or dictionary:

var tier_one_units = ["baby_ant", "worker_ant"]
var tier_two_units = ["angry_ant", "scientist_ant"]
var scene_tiers = {"tier_one": ["baby_ant", "worker_ant"],
	"tier_two": ["angry_ant", "scientist_ant"],
}

And use if/for statements to use the correct arrays.

Alternatively, you could keep their tier in the main dictionary and loop through the entire thing:

var blocks_scene_table = {
	"baby_ant" : {"weight" : 50000, "tier": 1},
	"worker_ant" : {"weight" : 30000, "tier": 1},
	"angry_ant" : {"weight" : 15000, "tier": 2},
	"scientist_ant" : {"weight" : 10000, "tier": 2},
	}


func _random_choose():
	var weight_sum = 0
	for enemy in blocks_scene_table:
		if blocks_scene_table[enemy]["tier"] <= max_enemy_tier:
			weight_sum += blocks_scene_table[enemy]["weight"]

Might get resource-intensive, depending on how large the blocks_scene_table gets, and how often you need to loop through it.

thank you, this was the method that was the one i understood the most.