+2 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 ?

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

+7 votes
Best answer

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

by (176 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())

or

var dict_val_array = Array(rooms_dic.values())

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

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