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).
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
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.
More on this: Invalid argument for "…()" function: argument 1 should be "…" but is "…" in Godot 4
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.
More on this: Invalid assignment of index '…' (on base: 'Array') with value of type '…' in Godot 4
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': falseRun 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.
More on this: How to parse JSON in Godot 4: JSON.parse_string()
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.
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.
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free