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 = 250Constants 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
EasyThe game does not start, because the script changes the constant SPEED. Fix it:
- Keep
SPEEDa 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Nodes and Scenes
1Your First Godot Game
What Is Godot?Ready Runs OncePrint in GodotThe Scene TreeSet a PositionMove a Node Once3Values You Can Tune
Variables on a NodeConstantsExport VariablesVector2 MathWrap Around the ScreenYour Own FunctionsPractice on your own: Godot playground