Menu
CoddyTech

Constants

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 14 of 27.

Some values never change while the game runs, like a robot's top speed. Use a constant for them. It is made with const instead of var:

const SPEED = 250

Constants are written in CAPITALS, so readers see the value is fixed. To change it, edit the number where it is made.

A line that changes a constant is an error. Godot catches it before the game starts. The Console says Cannot assign a new value to a constant and names the line.

challenge icon

Challenge

Easy

The game does not start, because the script changes the constant SPEED. Fix it:

  • Keep SPEED a constant.
  • Make the robot move left at 300 pixels a second.

Try it yourself

extends Node2D

const SPEED = 200

func _ready():
	SPEED = 300

func _process(delta):
	position.x -= SPEED * delta
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Nodes and Scenes

Practice on your own: Godot playground