godotengine / godotengine/godot

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

Open
#91,015 3 comments 0 reactions 0 assignees View on GitHub
discussion topic:gdscript
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:
![002051 - 2024-04-22](https://github.com/godotengine/godot/assets/111675193/6c9d3e7d-dea5-4863-89b4-258387edd39a)
```
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:
![002050 - 2024-04-22](https://github.com/godotengine/godot/assets/111675193/82364278-0320-40bc-8087-a7b14915b5be)
```
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.