Skip to content

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

By Eduardo Orellana · Updated

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].

AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.

Intel or AMD Chromebook, with Linux turned on.

Download for macOSDownload for WindowsDownload for Linux

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.

More on this: Unable to obtain iterator object of type 'Object' (but was obtained on first iteration?) in Godot 4

Reproduce it

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

GDScript
extends Node

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

What Godot 4.7.2 prints

Output
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.

GDScript
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

Output
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.

Prompt
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.

GDScript
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.

More on this: Invalid access to property or key on a base object of type 'previously freed' in Godot 4

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.

More on this: Invalid access to property or key '…' on a base object of type 'Dictionary' in Godot 4

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, gdscript_vm.cpp lines 1110 to 1133, the generic keyed get.

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.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free