Initialise an array in MMBasic and the obvious code is:
DIM INTEGER aactive(199)
FOR i = 0 TO 199
aactive(i) = 0
NEXT i
Fine, but unnecessary, because MMBasic ships the vector form: MATH SET. One line, same effect, runs in C instead of through the BASIC interpreter.
MATH SET 0, aactive()
First argument is the value, second is the target array. Works for INTEGER and FLOAT, one- or multi-dimensional, as long as the array was already declared with DIM.
Where it really pays off for me
On a game reset I have four pool arrays that all need to go to 0, plus a couple of value arrays:
SUB ResetGame()
MATH SET 0, aactive()
MATH SET 0, pactive()
MATH SET 0, shot_active()
MATH SET 0, ufo_shot_active()
' ...
END SUB
Before MATH SET that was four nested FOR loops or one big loop with multiple assignments inside. The code now reads like a list of intentions, not counter poetry. It’s not in the hot loop of a frame, but on game start or level change the time difference is measurable, and the flow in the code is just better.
Values other than 0
Not just zeroing, any value works. For instance if I want to pre-fill highscore slots with -1:
DIM INTEGER hs_levels(9)
MATH SET -1, hs_levels()
Or a float buffer with a sentinel:
DIM FLOAT distances(99)
MATH SET 9999.0, distances()
That’s the “empty memory with a sentinel” pattern, without two lines and a counter.
Multi-dimensional too
MATH SET fills the whole array regardless of dimensions. I have a vertex cache of 200×16 floats, one MATH SET 0, ... and it’s all zero:
DIM FLOAT vert_x(199, 15)
MATH SET 0, vert_x()
With FOR loops that would be two nested iterations over 200 and 16 elements.
Sibling commands
Once MATH SET is in muscle memory, the other MATH siblings start to make sense: MATH ADD for scalar addition across a whole array, MATH SCALE for scalar multiplication, MATH SLICE for pulling a row. Same pattern, all of them replace FOR loops, all of them run faster.
On MMBasic on the CMM2 or MMB4L it’s worth pausing before any array loop and asking whether MATH … already covers it. More often than I expected when I started.