0 votes

I want to go through a large dictionary of rooms.
The key value pair is Vector2() and a pointer to the room for each element.

This would be my first approach but I don't think that this would scale very well for a 1000+ room dictionary.

for i in rooms_dic.keys():

    var room = rooms_dic[i]

is there a way to get a array of the elements of a dictionary directly ?

asked Apr 10, 2016 in Engine by ulty (137 points)

Maybe you could have two representations of the container:
One to quickly iterate over all rooms (array), and another to quickly get a room from its position (dict).
Then you could maintain the two containers within a "room manager" or whatever class is in control of the rooms.

Probably the way I will implement it, but I was hoping that there was an easier (lazier) way to do it.

I'm assuming what Zylann said actually might be an answer, this is the way how I'm doing this thing as well.

2 Answers

+2 votes
Best answer

The newer version of Godot now has a dictionary.values() function so this has been perfectly solved.

answered Sep 5, 2016 by ulty (137 points)
+3 votes

So do you want to be 'fast' (as per your title) or 'lazy'? Anyway, you can be slightly lazy by doing

for i in rooms_dic:
    var room =  rooms_dic[i]

(i.e. don't need ".keys()").

You can get an array of the values or keys by doing

var dict_key_array = Array(rooms_dic.keys())


var dict_val_array = Array(rooms_dic.values())

and then iterate the array. What more did you want?

answered Apr 10, 2016 by duke_meister (708 points)
var dict_val_array = Array(rooms_dic.values())

would be perfect but as far as I can tell there is no such function ( values() ) on base dictionary

ahh.. so sorry.. too much jumping between languages :/

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 How to use this Q&A? before posting your first questions.