Trying to assign value of type 'String' to a variable of type 'int' in Godot 4
By Eduardo Orellana · Updated
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.
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': falseText 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.
Related
- Invalid operands 'String' and 'int' in operator '+'
- Trying to assign a non-object value to a variable of type
- Every verified Godot 4 answer
- Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
- Invalid cast: could not convert value to '…' in Godot 4
- Trying to assign an array of type "Array" to a variable of type "Array[…]" in Godot 4
- Invalid cast: can't convert a non-object value to an object type in Godot 4
- Invalid argument for "…()" function: argument 1 should be "…" but is "…" 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