Creating a 2 dimensional array from data within a dictionary (or alternative)?

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

Hi all,

I have a dictionary, it contains stuff like this:

(0:(name:Mary), (age:12), (1:(name:Sue), (age:13), (7:(name:CrazyLady), (age:14), (11:(name:Derick), (age:14))

It’s called dict.

You’ll note the ID for each one changes, it’s not 0,1,2,3 etc it’s potentially random due to various logic that creates it from a JSON file. So in that above example it’s 0,1,7,11.

What I want to do is be able to loop through them logically and call them one at a time sequentially when moving through a menu type system. So I was thinking I could create a new dictionary or array or similar that looks something like this:

0: 0
1: 1
2: 7
3: 11

That way, I could just call in order 0-3 (or whatever) and use that as the index to get the value from dict. So I could say use 2 in my array to retrieve 7 from dict.

I hope that makes sense :slight_smile:

Does anyone know a way to create a new array/dict with an incremental KEY and then feed in the ID from dict as its pair?

I’ve been looking at the array and dictionary examples but I’m not the sharpest tool in the shed I think. Anything to prompt me in the right direction and I’ll be away. Much appreciated…

:bust_in_silhouette: Reply From: Zylann

Where are you getting that order from in the first place?
In Godot 3.0 I know that dictionary iteration has been ordered by insertion order (I don’t know about 2.1.x), but if you want an actual array, I would do this:

var characters = []
for k in dict:
    characters.append(dict[k])

The characters array will then reference the data of every item in your dictionary sequentially (not including their ID, though. If you need them instead, then append k instead of dict[k])

But as I said, it’s possible the resulting order won’t be guaranteed in 2.x (but once it’s in the array order won’t change obviously, and you can sort it if needed, by name or ID).

Also it’s one-dimensional here, not sure what you meant by 2 dimensions :stuck_out_tongue:

In Godot 2.1.x order is not garanteed. The best way to keep an order on a dictionary is to use an array.

quijipixel | 2017-08-25 20:42

How I get that order:
I’m taking a JSON file and placing it into a dictionary.

I then filter that dictionary and only retrieve the entries that fit my requirements. So a dictionary with say, 100 items may only come down to 4 items. The items could have been taken anywhere in the dictionary. Hence they are not KEY’d 0,1,2,3 etc. They could be keyed anything. 2,5,3,56,12, etc.

Version
I should note I’m using 2.1.3

Your code

var characters = []
for k in dict:
    characters.append(dict[k])

Yes I am doing something similar, actually almost identical. I then do a print(k) and I get [3, 4, 0, 1] (in my example). so they’re not sequential as in 0,1,2,3.

So if you look at the original question you can see I’m trying to pair them to a sequential order, so I can then use a lookup. So I want them to output something like:

0: 0
1: 1 
2: 7
3: 11

The idea being in the end, that I want to press right arrow and move to next character with =+1 and pressing left will go to previous character with =-1`.

There MUST be a way to do it but I’m not great with arrays and dictionaries as yet. Maybe there’s ANOTHER way to do this? Is there a way I can move between the data (going to the next one with an input) without needing them to have perfectly sequential keys?

Robster | 2017-08-25 23:21

But… I don’t understand what’s your problem then. You have the solution right there :smiley:
Just keep that array next to your dictionary of filtered items (or don’t use the dictionary at all if you reference the characters in the array). Keep also the index of your current index in the array, and add 1 or -1 to move previous or next.

Zylann | 2017-08-26 21:58

You’re right. It should have worked and that’s why I was losing my mind.
It turns out after about FIVE hours! :slight_smile: I finally found the issue.

In a previous attempt at working on this I was setting a variable in another function that I was using in the array function and it was over writing it each time. It was doing so with a value from the dictionary so I thought it was how arrays/dictionaries must work and all I had learned about them was wrong. It was seriously melting my brain.

Then I found the rogue line of code and bam! she works!

Thanks so much for sticking through this. The advice given kept me pushing through until the rogue line was found.

Robster | 2017-08-27 23:00

:bust_in_silhouette: Reply From: Robster

EDIT: I thought I had an answer, I was so very VERY wrong. :slight_smile: