Skip to content
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

Make Array/Packed*Array slice consistent #3479

Closed
nathanfranke opened this issue Oct 29, 2021 · 1 comment
Closed

Make Array/Packed*Array slice consistent #3479

nathanfranke opened this issue Oct 29, 2021 · 1 comment
Milestone

Comments

@nathanfranke
Copy link
Contributor

Describe the project you are working on

godotengine/godot#35901

Note 1: All references in this proposal are after the changes in the above PR (e.g., end is exclusive).

Note 2: For those not familiar with it, Packed arrays are implemented as Vector internally, so whenever I say Vector I am referring to Packed*Array.

Describe the problem or limitation you are having in your project

Array.slice and Vector.slice behave differently

  1. Array takes begin, end, step, deep, Vector takes only begin and end.
  2. Array will never error unless step is invalid, while Vector fails if the indices are not -size() <= i < size().

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

  1. Remove step.
  2. All indices out of bounds will error: 0 <= i < size(), with the exception of -1 for end. I think this makes sense because:
    a) I find it unlikely that someone will pass -2 to mean the second to the last element, since that's kind of oddly specific.
    b) This way there can be a default value of -1.

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

var arr := []
arr.push_back(0)
arr.push_back(1)
arr.push_back(2)
arr.push_back(3)

print(arr.slice(1, 3)) # [1, 2]
print(arr.slice(0, -1)) # [0, 1, 2, 3]
print(arr.slice(0, -2)) # ERROR

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

Array and Vector are used often.

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

Array and Vector are part of core.

@Calinou Calinou added this to the 4.0 milestone Oct 29, 2021
@nathanfranke
Copy link
Contributor Author

Pretty much fixed as of godotengine/godot#35901

Only the end index can be negative. And there is still a step parameter, but it doesn't really hurt to keep it as of now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants