Arrays and Loops
Arrays become truly powerful when combined with loops. Instead of writing the same operation for each element by hand, a for loop can process the entire array in just a few lines.
Initializing an Array
If you needed to set all elements to the same value — say, zero — use a for loop:
int scores[10]int i
for i = 0 to 9 scores[i] = 0endThe loop variable i goes from 0 to 9, covering every valid index in a 10-element array.
Iterating Over an Array
Reading from an array follows the same pattern. This snippet prints all five weapon IDs on screen, one per second:
int weapons[5]
weapons[0] = 346weapons[1] = 347weapons[2] = 352weapons[3] = 355weapons[4] = 356
int ifor i = 0 to 4 print_formatted_now "Weapons[%d] = %d" 1000 i weapons[i] wait 1000endSearching an Array
A common task is to find a specific value in an array. Loop through the elements and check each one:
int weapons[5]
weapons[0] = 346weapons[1] = 347weapons[2] = 352weapons[3] = 355weapons[4] = 356
int ifor i = 0 to 4 if weapons[i] == 352 // UZI then print_formatted_now "UZI found at index %d" 2000 i break endendThe break exits the loop as soon as the UZI is found — no need to keep checking the rest.