# Invalid cast: could not convert value to '…' in Godot 4

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.

Source: https://flockbay.com/godot/invalid-cast-could-not-convert-value/ · Updated 2026-10-30

## 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".

## 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 3
```

## 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 cast: could not convert value to 'int'.". Find where it comes from and fix it. 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. Then run the game headless and check that Godot's output no longer shows it.
```

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

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: [Invalid cast: can't convert a non-object value to an object type in Godot 4](/godot/invalid-cast-cant-convert-a-non-object-value-to-an-object-type/), [Trying to assign value of type 'String' to a variable of type 'int' in Godot 4](/godot/trying-to-assign-value-of-type-string-to-int/), [Invalid type in static function 'from_angle' in type 'Vector2'. Cannot convert argument 1 from String to float in Godot 4](/godot/invalid-type-in-static-function-cannot-convert-argument/), [Invalid operands 'String' and 'int' in operator '+' in Godot 4](/godot/invalid-operands-string-and-int-in-operator-plus/), [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/)
