Hands-on: Bonus Counter
Let’s put this chapter’s concepts together. We will write a script that waits for a key press, then gives the player a cash bonus over several rounds with a countdown.
The final script will look like this:
{$CLEO .cs}nop
const ROUNDS = 5const BONUS = 250const KEY_START = 116 // F5const KEY_STOP = 117 // F6
// wait for F5 to startwhile not is_key_pressed KEY_START wait 0end
int ifor i = ROUNDS downto 1 // exit early if F6 is pressed if is_key_pressed KEY_STOP then break end
add_score 0 BONUS print_formatted_now "+$%d (%d rounds left)" 1000 BONUS i wait 1000end
print_string_now "All done!" 2000
terminate_this_scriptLet’s walk through it.
The first section declares constants for all configurable values. Changing ROUNDS or BONUS at the top adjusts the entire script behavior without touching the rest of the code:
const ROUNDS = 5const BONUS = 250const KEY_START = 116 // F5const KEY_STOP = 117 // F6Next, the script waits for the player to press F5. This is the same pattern used in the vehicle spawning script:
while not is_key_pressed KEY_START wait 0endThe main body is a for loop counting down from ROUNDS to 1. On each iteration, it adds money to the player’s account and displays a formatted message showing the bonus amount and remaining rounds:
int ifor i = ROUNDS downto 1 ... add_score 0 BONUS print_formatted_now "+$%d (%d rounds left)" 1000 BONUS i wait 1000endInside the loop, a break condition checks if F6 is pressed. If so, the loop exits early:
if is_key_pressed KEY_STOP then break endSave the file as bonus_counter.txt and press F7 to compile and copy. Start a new game (or load a save), press F5, and watch the bonuses roll in. Press F6 at any time to stop early.
What’s next?
Try these modifications and recompile:
- Change
ROUNDSto10andBONUSto100. - Replace
downtowithtoso the counter goes up instead of down. - Add
step 2to give a bonus every other round. - Instead of
break, try usingcontinueto skip a specific round (e.g. skip round 3).