Invalid operands 'Nil' and 'int' in operator '+' in Godot 4
By Eduardo Orellana · Updated
One side of the operator is null: a variable that was declared without a value, or a lookup that found nothing. hp + 10 with hp still null stops with Invalid operands 'Nil' and 'int' in operator '+'. Give the variable a value when you declare it, var hp := 100, or check it is not null before the arithmetic.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
Nil is the type of null in Godot. An untyped var declared with no value holds null, and no arithmetic operator is defined between Nil and a number, so the evaluator stops with the two type names.
A typed declaration avoids it: var hp: int starts at 0, not null, and var hp := 100 starts at 100.
Reproduce it
A health variable declared without a value, then used in a sum.
extends Node
var hp
func _ready() -> void:
print(hp + 10)
print("after")
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Invalid operands 'Nil' and 'int' in operator '+'.
at: _ready (res://answers/invalid-operands-nil-and-int/repro.gd:6)
GDScript backtrace (most recent call first):
[0] _ready (res://answers/invalid-operands-nil-and-int/repro.gd:6)Fix it
Declare it with a type and a starting value.
extends Node
var hp := 100
func _ready() -> void:
print(hp + 10)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
110A lookup that returned null
Dictionary.get(key) returns null for a missing key, and a function with no return statement on some path returns null. The arithmetic after it fails with 'Nil' on one side. Pass a default, stats.get("hp", 0), or return a value on every path.
var hp = stats.get("hp", 0)
print(hp + 10)
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 795, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_vm.cpp#L795).
Questions
- What is Nil in Godot?
- The type of null. An untyped variable with no value, a missing dictionary key read with get(), and a function that returns nothing all give Nil.
- Why did the editor not warn me?
- The variable is untyped, so the analyzer cannot know it is null when the line runs. Typing it, var hp: int, gives it 0 instead of null.
Related
- Invalid operands 'String' and 'int' in operator '+'
- Invalid access to property or key on a base object of type 'Nil'
- Every verified Godot 4 answer
- Invalid call. Nonexistent function '…' in base 'Nil' 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
- Cannot infer the type of "…" variable because the value is "null" in Godot 4
- Invalid assignment of property or key with value of type on a base object of type 'Nil' 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