Skip to content

Invalid assignment of index '…' (on base: 'Array') with value of type '…' in Godot 4

By Eduardo Orellana · Updated

The index is past the end of the array, and assigning to it does not grow the array. inventory[5] = item on a three-element array stops with Invalid assignment of index '5' (on base: 'Array') with value of type 'int'. Add elements with append(), or resize() the array first.

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

An Array assignment by index only replaces an element that exists. When the index is past the end, the setter reports failure and the VM prints this message with the index, the base type and the type of the value.

Typed arrays name their element type in the base: an Array[int] prints (on base: 'Array[int]'). The VM's source also has an Out of bounds set index message for this case; on 4.7.2 a plain index assignment on an Array printed Invalid assignment of index, typed or not, as the run shows.

More on this: How to remap input actions in code in Godot 4: InputMap and keycode

Reproduce it

A three-slot inventory, with a value written to slot 5.

GDScript
extends Node

func _ready() -> void:
	var inventory = [0, 0, 0]
	inventory[5] = 42
	print("after")

What Godot 4.7.2 prints

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

SCRIPT ERROR: Invalid assignment of index '5' (on base: 'Array') with value of type 'int'.
   at: _ready (res://answers/invalid-assignment-of-index-on-array/repro.gd:5)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/invalid-assignment-of-index-on-array/repro.gd:5)

Fix it

Grow the array before writing past its end, or append to it.

GDScript
extends Node

func _ready() -> void:
	var inventory = [0, 0, 0]
	if inventory.size() <= 5:
		inventory.resize(6)
	inventory[5] = 42
	print(inventory)
	inventory.append(7)
	print(inventory)

What the fixed script prints

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

[0, 0, 0, <null>, <null>, 42]
[0, 0, 0, <null>, <null>, 42, 7]

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.

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

Prompt
My Godot 4 project prints this error: "Invalid assignment of index '5' (on base: 'Array') with value of type 'int'.". Find where it comes from and fix it. The index is past the end of the array, and assigning to it does not grow the array. inventory[5] = item on a three-element array stops with Invalid assignment of index '5' (on base: 'Array') with value of type 'int'. Add elements with append(), or resize() the array first. Then run the game headless and check that Godot's output no longer shows it.

The same error in earlier Godot versions

Godot 4.2 and Godot 3.6 printed it as: Invalid set index '5' (on base: 'Array') with value of type 'int' The cause and the fix are the same.

More on this: Invalid assignment of property or key '…' with value of type '…' on a base object of type 'Node2D' 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 1023, the message, gdscript_vm.cpp lines 1010 to 1026, the keyed set.

Questions

Why does a Godot array not grow when I assign past its end?
Index assignment replaces an existing element. Growing is a separate call: append() adds one at the end, and resize() sets the size, filling new slots with null (or the type's default in a typed array).
What does resize() put in the new slots?
null in an untyped array, as the fix printed. A typed array such as Array[int] fills them with 0.

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

Download Flockbay free