Skip to content

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.

Download for macOSDownload for WindowsDownload for Linux

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.

More on this: How to get the current date and time in Godot 4: the Time singleton

GDScript
extends Node

func get_hp() -> int:
	var hp = "full"
	return hp

func _ready() -> void:
	print(get_hp())

What Godot 4.7.2 prints

Output
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)
0

Fix it

Return the declared type, and type the local so the analyzer can check it.

GDScript
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

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

100

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.

More on this: The "export" keyword was removed in Godot 4: every export(...) and its @export annotation

Prompt
My Godot 4 project prints this error: "Trying to return a value of type "String" from a function whose return type is "int".". Find where it comes from and fix it. 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. Then run the game headless and check that Godot's output no longer shows it.

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 2825, the message.

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.

Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.

Download Flockbay free