godotengine / godotengine/godot
Array.remove_at(index) out of bounds error even if the array size is correct
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### 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:

```
E 0:00:08:0882 Enemy.gd:163 @ _on_Player_move(): Index p_index = -1 is out of bounds (size() = 3).
./core/templates/cowdata.h:164 @ remove_at()
```
Using positive index:

```
E 0:00:11:0721 Enemy.gd:163 @ _on_Player_move(): Index p_index = 4 is out of bounds (size() = 4).
./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
Contributor guide
Research direction
Start with the reproduction using Array.remove_at(-1) and an index equal to the array size, then read the remove_at() implementation and bounds check in core/templates/cowdata.h. Compare the observed behavior with Array's documented index semantics and related tests or examples. Done means the reported cases have an agreed, verified behavior and any necessary test or documentation change is covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, godot
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100