godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add support for negative indices in `Array.remove` #4637

Open bend-n opened 2 years ago

bend-n commented 2 years ago

Describe the project you are working on

Chess, but can apply to many projects.

Describe the problem or limitation you are having in your project

Can't use .remove(-1) on *Arrays.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Looks clean, and it doesn't make sense that you can't use negative number indexing.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

[1, 2].remove(-1) [1, 2, 3].remove(-2)

If this enhancement will not be used often, can it be worked around with a few lines of script?

pop_back() is a viable alternative for -1, but does not apply to Pool*Arrays, and there are no alternatives to .remove(-5) [1, 2].remove([1, 2].size() -1) is possible, but it's rather verbose.

Is there a reason why this should be core and not an add-on in the asset library?

This is in array.

Its possible to make some kind of hacky function to do it, but as there is already a remove function, it should support it.

KoBeWi commented 2 years ago

Why not just pop_back()?

YuriSizov commented 2 years ago

I agree with KoBeWi that for -1 specifically we already have a method that works. If your intention is for general negative indices to be supported, that may be worth discussing. We do have some methods and accessors that support negative indices, so it wouldn't be that out of place.

bend-n commented 2 years ago

Why not just pop_back()?

pop_back() does not exist on Pool*Arrays

bend-n commented 11 months ago

We should just add pop_back for Packed*Array

AThousandShips commented 11 months ago

For Array you can just use pop_at for remove(-x)