Constants
So far, we’ve been using raw numbers directly in the code. For example, in the vehicle spawning script, the model number 415 appeared several times. If you wanted to change the vehicle, you’d have to find and update every occurrence. This is error-prone and makes the code harder to read.
A constant solves this problem. It is a named value that the compiler replaces with the actual value during compilation. Unlike a variable, a constant cannot be changed at runtime.
To declare a constant, use the const keyword:
const MODEL = 415Now, MODEL can be used anywhere in the code instead of 415:
const MODEL = 415
request_model MODEL
while not has_model_loaded MODEL wait 0end
Car vehicle = create_car MODEL at 2488.6963 -1660.1608 13.3359mark_model_as_no_longer_needed MODELIf you decide to switch from a Cheetah to a different car, you only need to update one line.
You can declare multiple constants on a single line, separated by commas:
const KEY_F5 = 116, KEY_F6 = 117Or use a const..end block for better readability:
const KEY_F5 = 116 KEY_F6 = 117 BONUS = 100endConstants can hold string values too:
const GREETING = "Hello!"const GXT_HELP = 'SAN_AND'