Cannot assign a new value to a constant in Godot 4
By Eduardo Orellana · Updated
SPEED is declared with const, and a constant's value is fixed when the script loads. SPEED = 20 fails to parse with Cannot assign a new value to a constant. Declare it with var if it changes during play, or keep the const and use a separate var for the current value.
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 const is evaluated once, when the script is compiled, and inlined where it is used. The analyzer refuses any assignment to it, so the script does not load.
Exported tuning values belong in @export var instead: they show in the Inspector, can differ per instance, and can change at run time.
Reproduce it
A constant speed changed in _ready().
extends Node
const SPEED = 10
func _ready() -> void:
SPEED = 20
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Cannot assign a new value to a constant.
at: GDScript::reload (res://answers/cannot-assign-a-new-value-to-a-constant/repro.gd:6)
ERROR: Failed to load script "res://answers/cannot-assign-a-new-value-to-a-constant/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
Keep the constant as the base value and change a var.
extends Node
const BASE_SPEED = 10
var speed := BASE_SPEED
func _ready() -> void:
speed = BASE_SPEED * 2
print(speed)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
20Verified 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_analyzer.cpp line 2933, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L2933).
Questions
- When should I use const instead of var?
- For values that never change while the game runs: tile sizes, layer numbers, preloaded scenes. Anything a level, a power-up or the player changes is a var.
- Can I change an element of a const array?
- No. Constant arrays and dictionaries are read-only too; changing an element fails at run time with Invalid assignment on read-only value.
Related
- Invalid assignment on read-only value (on base: 'Array')
- Identifier "…" not declared in the current scope
- Every verified Godot 4 answer
- Trying to assign a non-object value to a variable of type '…' in Godot 4
- Invalid assignment of index '…' (on base: 'Array') with value of type '…' in Godot 4
- Invalid assignment of property or key with value of type on a base object of type 'Nil' in Godot 4
- Trying to assign an array of type "Array" to a variable of type "Array[…]" in Godot 4
- Trying to assign value of type 'String' to a variable of type 'int' 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