Skip to content

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.

Download for macOSDownload for WindowsDownload for Linux

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().

GDScript
extends Node

const SPEED = 10

func _ready() -> void:
	SPEED = 20

What Godot 4.7.2 prints

Output
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.

More on this: Unable to iterate on object of type 'Nil' in Godot 4

GDScript
extends Node

const BASE_SPEED = 10
var speed := BASE_SPEED

func _ready() -> void:
	speed = BASE_SPEED * 2
	print(speed)

What the fixed script prints

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

20

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: Assertion failed in Godot 4

Prompt
My Godot 4 project prints this error: "Parse Error: Cannot assign a new value to a constant.". Find where it comes from and fix it. 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. 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_analyzer.cpp line 2933, the message.

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.

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

Download Flockbay free