|
|
|
|
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
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
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! 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