Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
By Eduardo Orellana · Updated
The function declares one return type and returned a value of another. A func get_hp() -> int that returns the String "full" stops with Trying to return a value of type "String" from a function whose return type is "int". Return the declared type, or change the declaration to match what the function returns.
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 function with a declared return type checks its return value when it returns. A String is not an int and does not convert to one implicitly, so the VM stops with both types. The caller then receives the type's default, 0 here, as the run shows.
When the analyzer can see the returned value's type, it refuses the script at parse time instead. Through an untyped local, as here, the check happens at run time.
More on this: Trying to call an async function without "await" in Godot 4
Reproduce it
A function typed -> int that returns text through an untyped local.
extends Node
func get_hp() -> int:
var hp = "full"
return hp
func _ready() -> void:
print(get_hp())
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Trying to return a value of type "String" from a function whose return type is "int".
at: get_hp (res://answers/trying-to-return-a-value-of-type-from-a-function-whose-return-type-is/repro.gd:5)
GDScript backtrace (most recent call first):
[0] get_hp (res://answers/trying-to-return-a-value-of-type-from-a-function-whose-return-type-is/repro.gd:5)
[1] _ready (res://answers/trying-to-return-a-value-of-type-from-a-function-whose-return-type-is/repro.gd:8)
0Fix it
Return the declared type, and type the local so the analyzer can check it.
extends Node
var max_hp := 100
func get_hp() -> int:
var hp: int = max_hp
return hp
func _ready() -> void:
print(get_hp())
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
100Verified 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 2825, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L2825).
More on this: The "export" keyword was removed in Godot 4: every export(...) and its @export annotation
Questions
- Can a function return null when its return type is a class?
- Yes. A function typed -> Node2D may return null. A built-in type such as int or Vector2 cannot be null.
- How do I return several types from one function?
- Leave the return type off, or return a Variant. You lose the check that caught this error.
Related
- Cannot get return value of call because it returns "void"
- Trying to assign value of type 'String' to a variable of type 'int'
- Every verified Godot 4 answer
- Invalid argument for "…()" function: argument 1 should be "…" but is "…" in Godot 4
- Cannot infer the type of "…" variable because the value is "null" in Godot 4
- Invalid cast: can't convert a non-object value to an object type in Godot 4
- Trying to assign a non-object value to a variable of type '…' in Godot 4
- Invalid cast: could not convert value to '…' 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