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

Array.remove_at(index) out of bounds error even if the array size is correct #91015

Open
Xerathe opened this issue Apr 22, 2024 · 2 comments
Open

Comments

@Xerathe
Copy link

Xerathe commented Apr 22, 2024

Tested versions

Godot Engine v4.0.2.stable.official.7a0977ce2
OpenGL API 3.3.0 - Build 21.20.16.4821 - Compatibility - Using Device: Intel - Intel(R) HD Graphics 520

System information

Windows 10

Issue description

Using remove_at method of Array throws out of bounds errors even if the size allows for specified index

Using remove_at with negative index:
002051 - 2024-04-22

E 0:00:08:0882   Enemy.gd:163 @ _on_Player_move(): Index p_index = -1 is out of bounds (size() = 3).
  <C++ Source>   ./core/templates/cowdata.h:164 @ remove_at()

Using positive index:
002050 - 2024-04-22

E 0:00:11:0721   Enemy.gd:163 @ _on_Player_move(): Index p_index = 4 is out of bounds (size() = 4).
  <C++ Source>   ./core/templates/cowdata.h:164 @ remove_at()

However, if using Array.erase(Array[-1]) no errors are shown.

Steps to reproduce

if keyboard button is pressed:

var a: Array = []
a.resize(10)
a.fill(1)
a.remove_at(-1)

Minimal reproduction project (MRP)

Not required

@JackyTruong
Copy link

JackyTruong commented Apr 22, 2024

Hi, for the remove_at() function in the documentation it states that negative indices don't work and that you would need to do arr.remove_at(arr.size() - (i + 1)). Similarly for removing at the size, array indices start counting at 0, so the final index would be a 3 not a 4 for size() = 4.

@RadiantUwU
Copy link
Contributor

RadiantUwU commented Apr 22, 2024

Hello! As the previous comment suggested, you cannot use negative indexes. It seems like you tried to pop back, i'd suggest you take a look at this method Array.pop_back() -> Variant

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

4 participants