-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exported dictionaries have their keys sorted alphabetically #90142
Comments
This is by design, for stability, as far as I know See also: |
should it happen with exported dictionaries though? I get why it needs to happen for dictionaries written to files and such, but i dont see why it should need to be the same with exported dictionaries? |
That's what exported dictionaries are though? They are stored and loaded |
but why should it be? Other than for stability as you mentioned, which seems only needed for file reading, i dont see any other reason why exported dictionaries should be sorted without the users knowledge. sort_custom should be used instead if the user or the engine REALLY has to have the dictionary sorted |
But they are exported, so they're stored, so they're sorted Are you printing it from the editor? Or from running project? If it's from the running project you are loading it, you have to load the scene? |
im running it from the project, but why then can it print the dictionary if its a hard coded variable, even a constant dosent have this issue, and wouldnt that be stored too? |
Keeping insertion order is a feature according to the documentation:
I don't think it should be sorted during serialization. Dictionary keeping insertion order is a result of 4.0 changing the implementation to use |
It also effects storing and recovering a dictionary via ConfigFile |
The cause of this issue is this line: https://github.com/godotengine/godot/blob/master/core/variant/variant_parser.cpp#L2248. I tried tracking down the original commit, but couldn't quite make it far back enough. It definitely feels... load-bearing, but I removed it on my build a few weeks ago, and it has even fixed similar issues as well (one of my exported dictionaries kept re-sorting its keys on every resource save, which would have caused endless merge conflicts for my team). I assume making this change would break existing test cases, but I agree that sorting dictionary keys at this stage of serialization should be considered a bug. |
Is there any workaround to this beyond recompiling? JSON-stringify I think loses a lot of information? I guess to use var_to_bytes, which is IIRC stable/in conformance with the documentation, but sacrificing human-readability/diffability? (I was working on an editor for my game, relying on the ordered behaviour of the Dictionaries for displaying properties in the object inspector, but the fields are shuffled about when I load them) |
I'm currently touching |
I am reluctant to deem this a "bug" given it's intentionally coded this way, but I've read enough of the above to deem this more annoying than it's worth. It's the only Variant type that does (or can do) this and it breaks user expectations. |
I had patched this issue locally for around 3 months and noticed no issues -- of course this is anecdotal and only covered my use cases, but it's enough for me to suggest stripping it for the 4.5 cycle, just to see if anything comes up |
no one has a easy way to keep dictionarys from sorting when saving a scene? |
As far as I have seen, no one has opened a PR addressing this issue yet. You could be the one. |
Okie Dokie |
As of Godot 4.4 with PR #77213, it is possible to manually sort when needed. In theory this means that it's not needed to do it automatically when serializing anymore, since if the user wants sorting, they can just call the function before serializing. However, in the past, |
Tested versions
System information
Godot v4.3.dev5.mono - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3070 Ti (NVIDIA; 31.0.15.4592) - AMD Ryzen 7 5800X 8-Core Processor (16 Threads)
Issue description
When an exported dictionary is created, they keys which are set in the editor, are sorted at runtime, which is incredibly frustrating for my use case.
Steps to reproduce
@export var dict := {}
)Minimal reproduction project (MRP)
N/A
The text was updated successfully, but these errors were encountered: