Assertion failed in Godot 4
By Eduardo Orellana · Updated
An assert() in your code was given a false condition. assert(hp > 0, "hp must be positive") with hp = 0 stops with Assertion failed: hp must be positive. The assert is doing its job: find why the condition is false at that point, here an hp that was never set above 0.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
assert(condition, message) checks the condition in debug builds. When it is false the VM stops the function with Assertion failed, followed by the message if one was given, and the debugger breaks there in the editor.
Asserts are compiled out of release exports: the condition is not even evaluated. Never put code with a side effect, such as a function that changes state, inside an assert.
Reproduce it
An assert that hp is positive, with hp still 0.
extends Node
var hp := 0
func _ready() -> void:
assert(hp > 0, "hp must be positive")
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Assertion failed: hp must be positive
at: _ready (res://answers/assertion-failed/repro.gd:6)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/assertion-failed/repro.gd:6)Fix it
Give hp its starting value before the code that relies on it.
extends Node
var hp := 0
func _ready() -> void:
hp = 100
assert(hp > 0, "hp must be positive")
print("hp ", hp)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
hp 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 3895, the message with your text](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L3895), [assert in the @GDScript class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/doc_classes/@GDScript.xml).
Questions
- Does assert() run in an exported game?
- Not in a release export. Asserts run in the editor and in debug exports only.
- What does Assertion failed. without text mean?
- The same check, from an assert() given no message. Add a message as the second argument so the error says what was expected.
Related
- Invalid operands 'Nil' and 'int' in operator '+'
- Trying to call an async function without await
- Every verified Godot 4 answer
- Cannot get return value of call to "…()" because it returns "void" in Godot 4
- Cannot assign a new value to a constant in Godot 4
- Invalid call to function '…' in base '…'. Expected N argument(s) in Godot 4
- Trying to return a value of type "…" from a function whose return type is "…" in Godot 4
- Cannot call method '…' on a null value 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