Open Solesaver opened 2 years ago
I think if you want to delete multiple elements, you could as well create a new array and give it only the non-removed elements (just be sure to not use append()
).
In Godot 4.0 similar result can be achieved with filter()
function.
Describe the project you are working on
I'm working on a simple infinite runner practice project to learn my way around the engine.
I'm currently working on some high score save/load functionality. At various times throughout the game's lifetime I'm running the high score array through a sanitization function. It sorts the array by score and then removes any duplicate names before cutting off any scores beyond the max record size.
Describe the problem or limitation you are having in your project
It is theoretically possible that before sanitization there are multiple duplicate names present that all need to be removed. My attempted implementation was to first search for all duplicates and store their indices, and then to remove all of them in a single pass. The remove function, unfortunately only supports a single index at a time, which wouldn't be efficient in this context.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
If a function in the remove family were added to Array that takes multiple indices to be removed it would support my desired functionality and avoid the bear trap of repeated inefficient calls to remove due to the necessary entry shifting for each removal.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
I'm sure there are bugs to be fixed and optimizations to make in that implementation, but that's more or less the helper function I'm about to implement for myself. Obviously if it's built into the array, some details will change.
If this enhancement will not be used often, can it be worked around with a few lines of script?
Depends on your definition of a few. The above can be implemented by any individual pretty easily, but it's fairly core Array functionality.
Is there a reason why this should be core and not an add-on in the asset library?
Array manipulation is a core part of said built-in type. Having it presented as an option in the core API is certain to save someone from a bear trap if they are trying to do any multi-removal. The naïve implementation of iterating through the array (possibly from the start at that) and removing items one at a time is O(n*m). The above algorithm keeps it O(n) plus sorting the indices, which could be removed and assumed/enforced.