Improve GDScript sorting mechanisms for Arrays of custom objects. #12894
TestSubject06
started this conversation in
Scripting
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We have a
sort_custom
in Array that allows us to sort arrays based on a callable, but because of the way it's implemented (quite necessarily) it causes a GDScript / C++ barrier crossing every time the callable is invoked. Even relatively small arrays (300-400 entries) take significant time to sort, where a very large amount of the time isn't spent sorting, but is instead spent crossing the language boundary.It would be nice if there was a way we could point at a specific field in the object, even if there were plenty of restrictions applied on the data types allowed to be used. Something like an
Array.sort_by("property")
.A thing I have to do a lot of in my projects is to sort a list of things by their distance to something of interest. Right now the fastest way to do that is to precalculate the distance for each thing, store that in a
Dictionary[float, Node]
, and then using.sort()
on that dictionary, which is not very intuitive - I only stumbled onto the sort method by accident. It just feels like that's not how that's supposed to be used.Alternatively it would be similarly useful to be able to reserve a property on custom objects where we can put a value that would be used by regular
sort()
calls, similar to how you can override the comparison operators in other languages - but without needing to open the whole can of worms that hidden control flow opens up. Imagine having asort_value
property or something similar that you can set before callingsort()
so that you don't have to cross the boundary so many times.Beta Was this translation helpful? Give feedback.
All reactions