# Invalid access of index '…' on a base object of type: 'Array' in Godot 4

The index is past the end of the array, and the array is in an untyped variable. var a = [1, 2, 3] then a[3] stops with Invalid access of index '3' on a base object of type: 'Array'. Indexes run from 0 to size() - 1, so check the index against a.size() first, or read the last element with a.back() or a[-1].

Source: https://flockbay.com/godot/invalid-access-of-index-on-array/ · Updated 2026-10-30

## What Godot is checking

When the compiler does not know a variable's type, a[3] compiles to a generic keyed get: the VM asks the value for the key at run time. An Array answers that it has no element 3, and the VM reports it as an invalid access of index, with the index and the base type in the message.

The message depends on what the compiler knew. The same read through a variable typed Array, with an int index, compiles to a typed indexed get and prints Out of bounds get index '3' (on base: 'Array') instead. It is the same mistake.

It is a runtime error: the function stops at that line, so the print("after") below it never ran.

## Reproduce it

An untyped array of three elements, read at index 3.

```
extends Node

func _ready() -> void:
	var scores = [10, 20, 30]
	print(scores[3])
	print("after")

```

## What Godot 4.7.2 prints

```
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

SCRIPT ERROR: Invalid access of index '3' on a base object of type: 'Array'.
   at: _ready (res://answers/invalid-access-of-index-on-array/repro.gd:5)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/invalid-access-of-index-on-array/repro.gd:5)
```

## Fix it

Check the index against size() before reading, and use back() for the last element.

```
extends Node

func _ready() -> void:
	var scores = [10, 20, 30]
	var i := 3
	if i < scores.size():
		print(scores[i])
	else:
		print("no score ", i, ", last is ", scores.back())

```

## What the fixed script prints

```
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

no score 3, last is 30
```

## Run this fix with your agent

Flockbay is the Godot editor with an AI agent built in. Paste this into Flockbay's chat, or into Claude Code or Codex connected to Flockbay over MCP: the agent finds the cause in your project, applies the fix, runs the game headless and reads Godot's output to check it.

```
My Godot 4 project prints this error: "Invalid access of index '3' on a base object of type: 'Array'.". Find where it comes from and fix it. The index is past the end of the array, and the array is in an untyped variable. var a = [1, 2, 3] then a[3] stops with Invalid access of index '3' on a base object of type: 'Array'. Indexes run from 0 to size() - 1, so check the index against a.size() first, or read the last element with a.back() or a[-1]. Then run the game headless and check that Godot's output no longer shows it.
```

## Off by one in a loop

A loop written for i in range(scores.size() + 1), or a while loop that tests i <= scores.size(), reads one element past the end on its last pass. Loop over range(scores.size()), or over the array itself.

```
for score in scores:
	print(score)

```

## An array that is shorter than you think

An array filled from get_children(), a file or a network message can be empty. scores[0] on an empty array fails the same way with index '0'. Test is_empty() before you read the first element.

## The same error in earlier Godot versions

Godot 4.2 and Godot 3.6 printed it as: Invalid get index '3' (on base: 'Array'). The cause and the fix are the same.

## Verified on Godot 4.7.2

Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.

Where the check lives in Godot's source: [gdscript_vm.cpp line 1130, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1130), [gdscript_vm.cpp lines 1110 to 1133, the generic keyed get](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1110-L1133).

## Questions

### Why does Godot 4 say Out of bounds get index in one script and Invalid access of index in another?

The compiler picks a different instruction when it knows the variable is an Array and the index is an int. A typed variable gives Out of bounds get index; an untyped one gives Invalid access of index.

### Do negative indexes work on a Godot array?

Yes. a[-1] is the last element and a[-2] the one before it. An index below -size() fails the same way as one past the end.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [Out of bounds get index '…' (on base: 'Array') in Godot 4](/godot/out-of-bounds-get-index/), [Invalid assignment of index '…' (on base: 'Array') with value of type '…' in Godot 4](/godot/invalid-assignment-of-index-on-array/), [Invalid access to property or key '…' on a base object of type 'Dictionary' in Godot 4](/godot/invalid-access-to-property-or-key-on-dictionary/), [Invalid assignment on read-only value (on base: 'Array') in Godot 4](/godot/invalid-assignment-on-read-only-value/), [Every verified Godot 4 answer](/godot/), [How do I see Godot’s output and errors in Flockbay?](/docs/how-do-i-see-godots-output-in-flockbay/)
