Assign a name to an array's element (EDIT: Dictionary)

I want to assign a name to the coordinates of several bombs that are on the game’s map. When you lose I want to show the bomb’s name.

I have every bomb coordinate stored in an array.
I thought it would be the easiest and cleanest if I can assign a name to each coordinate in an array like this:


But unfortunately, not possible.

Thanks for your help!

Hi,

What you’re looking for is a dictionary, not an array:

var bomb_names: Dictionary = {
    Vector2i(11, 8): "hans"
}
	
print(bomb_names[Vector2i(11, 8)])  # Output: hans

Keep in mind that dictionary keys must be unique. That means that, if two bombs have the same position, you’ll have a problem with that code. If that never happens, you should be fine.


A cleaner way of doing that is to store the name directly inside the bomb node. I don’t know if you have a class dedicated to your bombs, but if you do, you should definitely do that. Using a dictionary is fine for prototyping, though.

3 Likes

Thank you! Didn’t know this existed in Godot haha
I’m using a TileMap actually, so the bombs don’t exist as individual objects, that’s why i gotta do it this way
Thanks!

1 Like