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.
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.
Reproduce it
A three-slot inventory, with a value written to slot 5.
extends Node
func _ready() -> void:
var inventory = [0, 0, 0]
inventory[5] = 42
print("after")
What Godot 4.7.2 prints
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.
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
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]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.
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](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1023), [gdscript_vm.cpp lines 1010 to 1026, the keyed set](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1010-L1026).
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.
Related
- Out of bounds get index (on base: 'Array')
- Invalid access of index on a base object of type: 'Array'
- Every verified Godot 4 answer
- Invalid assignment on read-only value (on base: 'Array') in Godot 4
- Invalid assignment of property or key '…' with value of type '…' on a base object of type 'String' in Godot 4
- Invalid assignment of property or key with value of type on a base object of type 'Nil' in Godot 4
- Trying to assign an array of type "Array" to a variable of type "Array[…]" in Godot 4
- Invalid assignment of property or key '…' with value of type '…' on a base object of type 'Node2D' in Godot 4
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free