Invalid cast: could not convert value to '…' in Godot 4
By Eduardo Orellana · Updated
The value cannot be converted to the type after as. [1] as int stops with Invalid cast: could not convert value to 'int', because an Array has no conversion to int. Read the element you meant, picked[0] as int, or check the type with typeof() 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
as to a built-in type, on a value the analyzer cannot type, is checked when it runs: Variant is asked whether the value's type can convert to the target. A float converts to int, and so does a String in an untyped variable (text that is not a number gives 0), but an Array or a Dictionary converts to no number, and the VM stops naming the target type.
When the analyzer knows both types it checks at parse time instead: a String literal cast as int fails to load with Cannot cast a value of type "String" as "int".
More on this: Invalid type in static function '…' in type '…'. Cannot convert argument in Godot 4
Reproduce it
An array cast to int.
extends Node
func _ready() -> void:
var picked = [1]
var count = picked as int
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Invalid cast: could not convert value to 'int'.
at: _ready (res://answers/invalid-cast-could-not-convert-value/repro.gd:5)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/invalid-cast-could-not-convert-value/repro.gd:5)Fix it
Cast the element, not the array, and see what text casts give.
extends Node
func _ready() -> void:
var picked = [1]
print(picked[0] as int)
var a = "12"
var b = "twelve"
var c = 3.9
print(a as int, " ", b as int, " ", c as int)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
1
12 0 3Verified 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 1647, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1647).
Questions
- Why does "twelve" as int give 0 and no error?
- In an untyped variable a String can convert to int, so the cast runs; the conversion reads no digits and gives 0, as the fix printed. Use is_valid_int() when the text may not be a number.
- Does as int round a float?
- No, it truncates: 3.9 as int is 3, as the fix printed. Use roundi() to round.
Related
- Invalid cast: can't convert a non-object value to an object type
- Trying to assign value of type 'String' to a variable of type 'int'
- Every verified Godot 4 answer
- Invalid argument for "…()" function: argument 1 should be "…" but is "…" in Godot 4
- Invalid type in static function '…' in type '…'. Cannot convert argument in Godot 4
- Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
- Invalid operands 'String' and 'int' in operator '+' in Godot 4
- Cannot infer the type of "…" variable because the value is "null" 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