# Trying to assign value of type 'String' to a variable of type 'int' in Godot 4

The variable is typed int and the value is a String, and Godot does not convert text to a number on assignment. level = input with input = "2" stops with Trying to assign value of type 'String' to a variable of type 'int'. Convert it first: level = input.to_int(), or int(input).

Source: https://flockbay.com/godot/trying-to-assign-value-of-type-string-to-int/ · Updated 2026-10-30

## What Godot is checking

A typed variable checks every value assigned to it. An int and a float convert to each other, but a String converts to no number type implicitly, so the VM stops with both type names.

When the analyzer knows the value is a String it refuses the script at parse time instead. Through an untyped variable, as here, the check happens when the line runs.

## Reproduce it

A typed level variable given text read from somewhere untyped.

```
extends Node

func _ready() -> void:
	var level: int = 1
	var input = "2"
	level = input
	print("after")

```

## What Godot 4.7.2 prints

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

SCRIPT ERROR: Trying to assign value of type 'String' to a variable of type 'int'.
   at: _ready (res://answers/trying-to-assign-value-of-type-string-to-int/repro.gd:6)
   GDScript backtrace (most recent call first):
       [0] _ready (res://answers/trying-to-assign-value-of-type-string-to-int/repro.gd:6)
```

## Fix it

Convert the text to a number before assigning it.

```
extends Node

func _ready() -> void:
	var level: int = 1
	var input = "2"
	level = input.to_int()
	print("level ", level)
	print("is_valid_int: ", input.is_valid_int(), ", 'two': ", "two".is_valid_int())

```

## What the fixed script prints

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

level 2
is_valid_int: true, 'two': false
```

## 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: "Trying to assign value of type 'String' to a variable of type 'int'.". Find where it comes from and fix it. The variable is typed int and the value is a String, and Godot does not convert text to a number on assignment. level = input with input = "2" stops with Trying to assign value of type 'String' to a variable of type 'int'. Convert it first: level = input.to_int(), or int(input). Then run the game headless and check that Godot's output no longer shows it.
```

## Text from a LineEdit, a file or JSON

LineEdit.text, FileAccess.get_line() and a ConfigFile value are Strings. Convert them with to_int() or to_float(), and check is_valid_int() first when a person typed the text, because "two".to_int() is 0.

## 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 1441, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L1441).

## Questions

### What does to_int() return for text that is not a number?

0, with no error. Check is_valid_int() first if 0 is a real value in your game.

### Does a float convert to int on assignment?

Yes, implicitly, and the fraction is dropped. Only the number types convert to each other this way.

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: [Trying to assign a non-object value to a variable of type '…' in Godot 4](/godot/trying-to-assign-a-non-object-value-to-a-variable-of-type/), [Trying to assign an array of type "Array" to a variable of type "Array[…]" in Godot 4](/godot/trying-to-assign-an-array-of-type-to-a-variable-of-type-array/), [Trying to return a value of type "…" from a function whose return type is "…" in Godot 4](/godot/trying-to-return-a-value-of-type-from-a-function-whose-return-type-is/), [Invalid cast: could not convert value to '…' in Godot 4](/godot/invalid-cast-could-not-convert-value/), [Invalid argument for "…()" function: argument 1 should be "…" but is "…" in Godot 4](/godot/invalid-argument-for-function-argument-should-be/), [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/)
