Quick answer: := tells GDScript to copy the variable’s type from the value on the right while the script is parsed. The error means that value has no static type: it came out of a Dictionary or an untyped Array, from an untyped variable or function, from a property read through a base class such as Node, or from an awaited signal. Write the type yourself (var hp: int = data["hp"]), cast with as, or give the source a type.

This is a parse error, so the script does not load and the node it is attached to does nothing until the line is fixed. The value on the right is usually fine at runtime. The problem is that Godot has to decide the variable’s type before the game runs, and nothing on the right side tells it what that type is. This guide lists where untyped values come from, shows how to find the untyped part of a long expression, and gives a fix for each case. It also covers the two neighboring messages, one for null and one for engine functions such as min(), because they have the same cause and different wording.

The exact error

The script editor underlines the value after := and shows this in the status line under the code:

Cannot infer the type of "hp" variable because the value doesn't have a set type.

Run the project anyway and the log shows the script failing to load:

SCRIPT ERROR: Parse Error: Cannot infer the type of "hp" variable because the value doesn't have a set type.
          at: GDScript::reload (res://player.gd:12)
ERROR: Failed to load script "res://player.gd" with error "Parse error".

The quoted name is the variable being declared. The word after it is variable, parameter (a default argument written with :=) or constant. The wording is the same from Godot 4.0 through 4.6. Godot 3 reports the same mistake as The assigned value doesn't have a set type; the variable type can't be inferred.

Two related messages come from the same := rule:

Cannot infer the type of "target" variable because the value is "null".
The variable type is being inferred from a Variant value, so it will be typed as Variant. (Warning treated as error.)

The first appears for var target := null. The second is the INFERENCE_ON_VARIANT warning, which Godot 4 raises to an error by default. It appears when the right side is an engine function that is declared to return a Variant, such as min() or Dictionary.get().

What := asks Godot to do

GDScript has three ways to declare a variable, and they differ in who decides the type. The last row of the table is the second way with a cast added.

DeclarationType of the variableIf the value has no static type
var hp = data["hp"]None. The variable can hold anything and each use is checked when it runsAccepted
var hp := data["hp"]Whatever type the right side has when the script is parsedThis error
var hp: int = data["hp"]int, because you wrote itAccepted. The value is converted or rejected at runtime
var hp := data["hp"] as intint, taken from the castAccepted

A static type is a type the analyzer knows without running the game. Literals have one: 100 is an int, "Rook" is a String, Vector2.ZERO is a Vector2. So do typed variables, functions with a return type annotation, and the methods and properties of engine classes. A Dictionary does not say what its values are, so data["hp"] could be a number, a string or another Dictionary, and := has nothing to copy.

One detail surprises people. An untyped variable has no static type even when its value is obvious:

var speed = 300          # untyped: may be reassigned to anything later
var boosted := speed     # Cannot infer the type of "boosted" variable ...

Godot remembers that speed started as an int and uses that for autocompletion, but it does not treat it as a guarantee. Only a declared or inferred type (var speed: int = 300 or var speed := 300) counts.

Where untyped values come from

Right-hand sideExampleMessage
An element of a Dictionary or an untyped Arrayvar hp := save["hp"], var first := items[0]doesn’t have a set type
An untyped variable or parametervar boosted := speeddoesn’t have a set type
A function of yours with no return typevar dmg := roll_damage()doesn’t have a set type
A property or method the known base class does not havevar tex := $Sprite2D.texture, var pos := get_parent().positiondoesn’t have a set type
An awaited signalvar choice := await dialog.choice_madedoesn’t have a set type
The loop variable of a for over an untyped Arrayfor item in items: then var n := itemdoesn’t have a set type
Any expression that contains one of the abovevar total := save["hp"] + 5doesn’t have a set type
The literal nullvar target := nullthe value is “null”
An engine function declared to return Variantvar low := min(a, b), var hp := save.get("hp", 100), var data := JSON.parse_string(text)inferred from a Variant value (warning treated as error)

The fourth row is the one that catches people most often. $Sprite2D and get_node() are typed as Node, and get_parent() returns Node. Godot does not open the scene to see what the node really is. Node has no texture or position, so the property lookup is left for runtime and its result has no type. For the same reason @onready var sprite := $Sprite2D compiles but gives you a variable of type Node, and the error shows up later on the line that reads sprite.texture.

Find the untyped part of the expression

  1. Read the right-hand side from left to right and stop at the first step whose type you could not state yourself. In get_parent().inventory.items[0] that step is .inventory, because get_parent() only promises a Node.
  2. If you cannot tell, split the line. Give each step its own := variable and see which line gets the error (example below).
  3. Hold Ctrl (Cmd on macOS) and click a function or variable name to jump to its declaration. A function without -> Type after its parameter list, or a variable declared with a plain =, is the source.
  4. Look at the line numbers. With Editor Settings > Text Editor > Appearance > Gutters > Highlight Type Safe Lines enabled (it is on by default), a line that Godot can fully type-check gets a green line number. A line number that keeps the normal color has something untyped on it, which is a quick way to see where typing stops in a function.

Splitting the example from step 1 looks like this:

var parent := get_parent()        # fine: Node
var inv := parent.inventory       # error here: Node has no "inventory"

Fix 1: write the type yourself

When you know what the value will be, replace := with a type annotation. This is the right fix for data that only exists at runtime: save files, JSON, dictionaries of settings.

var save: Dictionary = {"hp": 80, "name": "Rook"}

func _ready() -> void:
    # var hp := save["hp"]          # error
    var hp: int = save["hp"]
    var hero_name: String = save["name"]
    print(hero_name, " has ", hp, " hp")

The annotation moves the check from parse time to runtime. When the line runs, Godot converts between int and float if needed and stops on anything else with Trying to assign value of type 'String' to a variable of type 'int'. That case has its own guide. Values that come from JSON.parse_string() are a common source of surprises here, because JSON numbers arrive as float: annotating the variable as int converts 80.0 to 80 for you.

Two more cases take the same fix:

var target: Node2D = null                      # instead of := null
var choice: int = await dialog.choice_made     # instead of := await ...

A const Dictionary read with a literal key is the exception that needs no annotation. Godot evaluates const STATS = {"hp": 80} and STATS["hp"] while parsing, so var hp := STATS["hp"] is accepted and typed as int.

Fix 2: cast with as

For nodes and other objects, as states the type inside the expression, so you can keep :=:

extends Node2D

@onready var sprite := $Sprite2D as Sprite2D
@onready var health_bar := %HealthBar as ProgressBar

func _ready() -> void:
    var tex := sprite.texture           # Texture2D
    var player := get_parent() as Player
    if player:
        health_bar.max_value = player.max_health

With an object type, as returns null when the node is not of that class, so check the result before using it when the node might be something else. Writing the annotation instead (@onready var sprite: Sprite2D = $Sprite2D) is equivalent for the analyzer, but a wrong node type then stops the game with an assignment error instead of giving you null. Pick the behavior you want.

If Player or another class name in the cast is not recognized, the script needs a class_name line or a preload() constant. See the “Could not find type” guide.

Fix 3: give the source a type

When the same untyped value is read in many places, type it once where it is declared and every := downstream starts working.

Return types on your functions

# Before: callers get an untyped value
func roll_damage():
    return randi_range(4, 9)

# After
func roll_damage() -> int:
    return randi_range(4, 9)

func attack() -> void:
    var dmg := roll_damage()    # int

Member variables other scripts read

# player.gd
class_name Player
extends CharacterBody2D

var health := 100          # typed as int, so "var hp := player.health" works elsewhere
var max_health: int = 100
var display_name = "Rook"  # untyped: "var n := player.display_name" fails

Typed arrays and typed dictionaries

var enemies: Array[Enemy] = []
var prices: Dictionary[String, int] = {"sword": 120}    # Godot 4.4 and later

func _ready() -> void:
    var first := enemies[0]        # Enemy
    var cost := prices["sword"]    # int
    for enemy in enemies:
        var hp := enemy.health     # enemy is typed as Enemy

Typed arrays exist in every Godot 4 release. Typed dictionaries were added in Godot 4.4; on earlier versions, annotate the variable that receives the value instead. When you loop over an array you cannot type, Godot 4.2 and later let you type the loop variable: for item: Item in items:.

Parameters

func apply_damage(amount: int) -> void:
    var doubled := amount * 2    # int, because amount is typed

The editor can write some of these for you. With Editor Settings > Text Editor > Completion > Add Type Hints enabled, autocompleting a function override inserts the parameter and return types, and dragging a node from the Scene dock into the script while holding Ctrl (Cmd on macOS) creates a typed line such as @onready var sprite_2d: Sprite2D = $Sprite2D.

Fix 4: use the typed version of Variant functions

Several global math functions accept any type and are declared to return Variant, so := produces the “inferred from a Variant value” message. Each has typed versions that return int or float:

Returns VariantTyped versions
min(), max()mini(), minf(), maxi(), maxf()
clamp()clampi(), clampf()
abs(), sign()absi(), absf(), signi(), signf()
floor(), ceil(), round()floori(), floorf(), ceili(), roundi()
snapped(), wrap(), lerp()snappedi(), snappedf(), wrapi(), wrapf(), lerpf()
func update_bar(hp: int, max_hp: int) -> void:
    # var shown := clamp(hp, 0, max_hp)          # inferred from a Variant value
    var shown := clampi(hp, 0, max_hp)           # int
    var ratio := minf(float(hp) / max_hp, 1.0)   # float
    print(shown, " ", ratio)

Other engine methods return Variant and have no typed twin: Dictionary.get(), Array.pop_back(), pop_front(), front(), back() and pick_random(), Object.get(), get_meta(), call(), Callable.call(), JSON.parse_string() and str_to_var(). For those, annotate or cast as in the first two fixes:

var parsed: Variant = JSON.parse_string(text)    # null when the text is not valid JSON
var next := queue.pop_front() as Enemy
var volume: float = settings.get("volume", 1.0)

Annotating with Variant is allowed and is the honest type for JSON.parse_string(): test the result with if parsed is Dictionary: before reading keys from it.

You can lower this one message under Project > Project Settings > Debug > GDScript > Inference on Variant (turn on Advanced Settings if the section is hidden) from Error to Warn or Ignore. The line then compiles, and the variable is quietly a Variant, which is the opposite of what := was written for. Fixing the line is better. The “doesn’t have a set type” and “null” messages are plain errors with no setting.

How to confirm the fix

Save the script. The red marker and the message under the code disappear, and with type-safe line highlighting on, the line number turns green once every part of the line is typed. Then run the scene that executes the line. An annotation or a builtin cast such as as int replaces a parse-time check with a runtime one, so the first real test is the moment actual data flows through it: load an old save file, parse the real JSON, enter the area with the other kind of body. A node cast with as that yields null shows up one line later as an error about a base object of type ‘Nil’, which tells you the node was not the class you named.

What reaches players

The inference error is raised by the editor before the game runs, and Bugnet has no part in it. What can reach players is the runtime side of the fix. Once a line reads var hp: int = save["hp"], a save file or server response with the wrong kind of value fails on a player’s machine, where you cannot see it. In a debug build that failure is written to the engine’s log, and Bugnet’s Godot SDK builds its automatic error capture on that log file, so with File Logging enabled (it is off by default on mobile exports) it can arrive as a report with platform, OS and game version, stacked with other reports of the same title. Release export templates leave out much of GDScript’s runtime checking, so do not count on a log line there.

For data you do not control, check the type yourself and report the bad case by hand:

func read_hp(save: Dictionary) -> int:
    var raw: Variant = save.get("hp")
    if raw is int or raw is float:
        return int(raw)
    Bugnet.report_error("Save file: hp is not a number", "read_hp() got " + var_to_str(raw))
    return 100

The fixed message keeps every occurrence in one entry with a count, and the second argument carries the value that was found. Setup is in the getting started guide.

Frequently asked questions

What does Cannot infer the type of variable because the value doesn't have a set type mean in Godot?

The variable was declared with :=, which copies its type from the value on the right while the script is parsed, and that value has no type Godot can know in advance. It is a parse error, so the script does not load until you write the type yourself, cast the value with as, or give the source of the value a type.

What is the difference between = and := in GDScript?

A plain = leaves the variable untyped, so it can hold anything and each use is checked at runtime. := gives the variable the static type of the assigned value, exactly as if you had written the type by hand, and fails when the value has no static type. Writing var hp: int = value sets the type explicitly and works with untyped values.

Why does var x := min(a, b) give an error in Godot 4?

min(), max(), clamp() and several other global functions are declared to return Variant, so := would type the variable as Variant. Godot reports that as the INFERENCE_ON_VARIANT warning, which is treated as an error by default. Use the typed versions such as mini(), maxf() or clampi(), which return int or float.

Why can Godot not infer the type of $Sprite2D.texture?

The $ shorthand and get_node() are typed as Node, because Godot does not open the scene to see what the node really is. Node has no texture property, so the lookup is left for runtime and its result has no type. Cast the node, for example $Sprite2D as Sprite2D, or declare the variable as @onready var sprite: Sprite2D = $Sprite2D.

How do I give a type to a value read from a Dictionary in GDScript?

Annotate the variable that receives it, for example var hp: int = save["hp"], or cast with as. In Godot 4.4 and later you can also type the dictionary itself, such as Dictionary[String, int], and then := works on its values. The annotation is checked when the line runs, so a value of the wrong kind stops the game there.

The := operator needs a type it can read at parse time. When the right side comes from a Dictionary, an untyped function or a node known only as Node, write the type yourself or cast with as.