0 votes

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 :-)

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

in Engine by (810 points)

2 Answers

+2 votes

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 :p

by (27,172 points)

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

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?

But... I don't understand what's your problem then. You have the solution right there :D
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.

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.

0 votes

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

by (810 points)
edited by
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.
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 webmaster@godotengine.org with your username.

Categories