Hands-on: Vehicle Roulette
Time to combine functions and arrays into something fun. We’ll build a vehicle roulette — press a key, and the script picks a random vehicle from a pool and spawns it right next to you. Press again to swap it for another. Will you get a sports car or a tractor? Only one way to find out.
Here’s the final script:
{$CLEO .cs}
const KEY_SPIN = 116 // F5const NUM_VEHICLES = 8
// vehicle model poolint models[NUM_VEHICLES]models[0] = 415 // cheetahmodels[1] = 451 // turismomodels[2] = 522 // nrg-500models[3] = 432 // rhinomodels[4] = 509 // bikemodels[5] = 510 // mountain bikemodels[6] = 539 // vortexmodels[7] = 531 // tractor
Car currentCarint currentModel = -1
while true wait 0
if not is_key_just_pressed KEY_SPIN then continue end
// remove previous vehicle if it exists if currentModel <> -1 then cleanup(currentCar, currentModel) end
// pick a random vehicle int index = generate_random_int_in_range {min} 0 {max} NUM_VEHICLES currentModel = models[index]
// load, spawn, show loadModel(currentModel) currentCar = spawnNearPlayer(currentModel)
print_formatted_now "You got model %d! Press F5 for another spin." 3000 currentModelend
function loadModel(modelId: int) request_model modelId while not has_model_loaded modelId wait 0 endend
function spawnNearPlayer(modelId: int): int float pos[3] Char playerChar = get_player_char {player} 0 ...pos = get_offset_from_char_in_world_coords playerChar {xOffset} 0 {yOffset} 5.0 {zOffset} 0
Car vehicle = create_car modelId {at} ...pos return vehicleend
function cleanup(vehicle: Car, modelId: int) delete_car vehicle mark_model_as_no_longer_needed modelIdendLet’s break it down.
The Vehicle Pool
An array stores the model IDs of all candidate vehicles. Adding or removing vehicles is as simple as editing this list and updating NUM_VEHICLES:
const NUM_VEHICLES = 8
int models[NUM_VEHICLES]models[0] = 415 // cheetahmodels[1] = 451 // turismomodels[2] = 522 // nrg-500models[3] = 432 // rhinomodels[4] = 509 // bikemodels[5] = 510 // mountain bikemodels[6] = 539 // vortexmodels[7] = 531 // tractorThe mix of sports cars, bikes, a tank, and a tractor makes each spin genuinely unpredictable.
Three Functions
The script defines three focused functions, each handling one responsibility:
loadModel — The familiar request-and-wait pattern from the functions lesson, packaged for reuse:
function loadModel(modelId: int) request_model modelId while not has_model_loaded modelId wait 0 endendspawnNearPlayer — Creates the car in front of the player, and returns the handle:
function spawnNearPlayer(modelId: int): int float pos[3] Char playerChar = get_player_char {player} 0 ...pos = get_offset_from_char_in_world_coords playerChar {xOffset} 0 {yOffset} 5.0 {zOffset} 0
Car vehicle = create_car modelId {at} ...pos return vehicleendcleanup — Deletes the old vehicle and releases its model to free memory:
function cleanup(vehicle: Car, modelId: int) delete_car vehicle mark_model_as_no_longer_needed modelIdendEach function is small, readable, and does one thing.
The Main Loop
The script uses while true with a key-press check — the same pattern from Chapter II. The is_key_just_pressed condition fires only once per press, preventing a held key from triggering multiple spins:
while true wait 0
if not is_key_just_pressed KEY_SPIN then continue end ...endWhen the player presses F5, the script cleans up the previous car (if any), picks a random index from the models array, and calls the functions in sequence:
int index = generate_random_int_in_range {min} 0 {max} NUM_VEHICLEScurrentModel = models[index]
loadModel(currentModel)currentCar = spawnNearPlayer(currentModel)generate_random_int_in_range picks an index between 0 (inclusive) and NUM_VEHICLES (exclusive). That index pulls a model ID from the array, which flows through loadModel and spawnNearPlayer.
Save the file as vehicle_roulette.txt, press F7, and try your luck!
What’s next?
Try these modifications and recompile:
- Add more vehicles to the pool (find model IDs in the vehicles.ide).
- use get_name_of_vehicle_model and show the vehicle name instead of the model number
- Add a spin counter and print “Spin #N” with each roll.
- Make it cost $500 per spin — check the player’s money before proceeding, and show a “Not enough cash!” message if they can’t afford it.