# Cannot assign a new value to a constant in Godot 4

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.

Source: https://flockbay.com/godot/cannot-assign-a-new-value-to-a-constant/ · Updated 2026-10-30

## 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

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.

```
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](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.

Download Flockbay free: Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot. https://flockbay.com/download/

Related: [Invalid assignment on read-only value (on base: 'Array') in Godot 4](/godot/invalid-assignment-on-read-only-value/), [Identifier "…" not declared in the current scope in Godot 4](/godot/identifier-not-declared-in-the-current-scope/), [Cannot infer the type of "…" variable because the value is "null" in Godot 4](/godot/cannot-infer-the-type-because-the-value-is-null/), [Expected end of statement after variable declaration, found "Identifier" instead in Godot 4](/godot/expected-end-of-statement-after-variable-declaration/), [Every verified Godot 4 answer](/godot/), [How do I see Godot’s output and errors in Flockbay?](/docs/how-do-i-see-godots-output-in-flockbay/)
