Const name = value[,name2 = value2,...]

Parameters

name - the constant's name, with an optional type tag ($ for a string, # for a float)

value - what the name stands for; must be something the compiler can work out on its own

Description

Declares a named constant - a value that is fixed when the program is built.

Const SCREEN_W = 800 lets you write SCREEN_W everywhere instead of 800, and change it in one place later. Tile sizes, scancodes, gravity, the number of lives you start with - anything you tune while making the game is better as a constant than as a number scattered through the code.

Constants cost nothing at runtime. The compiler substitutes the value wherever the name appears, so there is no variable to look up and no memory used. Trying to assign to one is a compile error, which is rather the point: a constant cannot drift.

Declare several on one line with commas: Const TILE = 32, MAP_W = 40, MAP_H = 30. Add a tag for a string or float constant: Const TITLE$ = "My Game", GRAVITY# = 0.5.

Two rules to remember. A constant must be given a value ("Constants must be initialized"), and that value has to be a constant expression - a literal, or arithmetic on literals and other constants. A variable or a function call will not compile. Constants can also only be declared in the main program, never inside a Function, although once declared they are visible everywhere, functions included.

See also: Global, Local, Dim, Data.

Example

; Const Example
; -------------

; Const declares a value that can never change - perfect for game rules
Const MAX_LIVES=3
Const ALIEN_SCORE=100
Const GRAVITY#=9.8

Print "You start with "+MAX_LIVES+" lives."
Print "Every alien you shoot is worth "+ALIEN_SCORE+" points."
Print "Gravity pulls your ship down at "+GRAVITY+" units per second."

; Constants can be used anywhere a value can
Print "Shooting 5 aliens scores "+(5*ALIEN_SCORE)+" points."

Print ""
Print "Press any key to close the example"
WaitKey

End

Index