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.

Download for macOSDownload for Windows

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

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.