Menu

GDScript Constants: const, preload and Read-Only Data

How const works in GDScript: naming, typed constants, constant arrays and dictionaries that are read-only, preload constants, and the built-in constants PI, TAU and INF.

This page includes runnable editors. Edit, run, and see output instantly.

A constant is a name for a value that never changes. Declare it with const:

Star Hopper: 100/100 HP, gravity 980.0

Constants use UPPER_SNAKE_CASE. Like variables, they may carry a type (: String) or infer one (:=).

A Constant Cannot Change

Assigning to a constant is an error the editor shows at once:

const MAX_HEALTH = 100

func _ready():
	MAX_HEALTH = 200
	# Parse Error: Cannot assign a new value to a constant.

Its value must also be known before the game starts. Numbers, strings, other constants, math on them and a few built-in functions work. Anything that needs the running game does not: const START = Time.get_ticks_msec() is an error, so use a var for it.

Constant Arrays and Dictionaries Are Read-Only

A constant array or dictionary cannot be changed, not even with a method like append():

cave costs nothing, a sword costs 50
3 levels
true

Calling LEVELS.append("tower") here prints Array is in read-only state. as an error and the array stays the same. If you need a list that grows, declare it with var, or copy the constant: var levels := LEVELS.duplicate().

Constants on a Class

A constant belongs to its class, so other scripts can read it through the class name:

40

The same works with a script that has a class_name: Player.MAX_SPEED from any other script. See classes.

const with preload

preload() loads a file when the script loads, so it fits in a constant. This is the usual way to keep a scene you spawn often:

const BulletScene = preload("res://scenes/bullet.tscn")

func shoot():
	var bullet := BulletScene.instantiate()
	add_child(bullet)

The name is often written in PascalCase when the constant holds a scene or a class, because you use it like a type. When the path is only known at run time, use load(path) and a variable instead.

Built-in Constants

Godot defines a few math constants you can use anywhere:

3.14159265358979
6.28318530717959
true
true

TAU is 2 * PI, one full turn in radians. Enum values are constants too: see enums.

Frequently Asked Questions

How do I declare a constant in GDScript?

Use const with a value: const MAX_SPEED = 400.0. A constant must get its value when it is declared, and that value must be known before the game runs. Names are written in UPPER_SNAKE_CASE by convention.

What is the difference between const and var in GDScript?

A var can be reassigned while the game runs. A const never changes, is computed once when the script loads, and can be used by other scripts through the class name, as in Player.MAX_SPEED.

Can a constant array be changed in GDScript?

No. An array or dictionary stored in a const is read-only. Calling append() on it prints "Array is in read-only state." and leaves it unchanged. Use var for a list you want to change.

Should I use const or preload for scenes?

Both together: const BULLET = preload("res://bullet.tscn") loads the scene once, when the script loads, and gives it a fixed name. Use load() in a var when the path is only known at run time.

Coddy programming languages illustration

Learn to code with Coddy

Get started