Games constantly spawn and retire objects, shots, particles, fragments, sometimes a hundred alive at once. In modern languages that’s new and free, or a dynamic list. MMBasic doesn’t really offer either as a practical game solution. And even if it did, per-frame allocation would be too expensive. Here’s the pool pattern I built for my game, the one no tutorial covers but that keeps my game loop alive.
Object pool
My approach is a fixed pool: at startup I reserve the maximum number of objects that should ever live at the same time, once. Each slot has an aactive flag. “Active” is a flag toggle, not a creation or destruction event.
CONST MAX_PARTICLES = 200
DIM FLOAT px(MAX_PARTICLES-1), py(MAX_PARTICLES-1)
DIM FLOAT pvx(MAX_PARTICLES-1), pvy(MAX_PARTICLES-1)
DIM INTEGER plife(MAX_PARTICLES-1), pactive(MAX_PARTICLES-1)
On reset: MATH SET 0, pactive(). All slots free in one shot, no FOR loop.
Finding a free slot
When a new shot or particle needs to spawn, I scan for the first free slot:
FUNCTION FindFreeSlot() AS INTEGER
LOCAL INTEGER i
FOR i = 0 TO MAX_PARTICLES - 1
IF NOT pactive(i) THEN
FindFreeSlot = i
EXIT FUNCTION
END IF
NEXT i
FindFreeSlot = -1
END FUNCTION
Returns -1 when the pool is full. The new particle just gets dropped, you can’t tell visually.
Updating means walking the active slots
Inside the game loop I iterate over every slot and check the flag:
FOR i = 0 TO MAX_PARTICLES - 1
IF pactive(i) THEN
INC px(i), pvx(i)
INC py(i), pvy(i)
INC plife(i), -1
IF plife(i) <= 0 THEN
pactive(i) = 0
ELSE
PIXEL px(i), py(i), COL_WHITE
END IF
END IF
NEXT i
When a particle expires, pactive flips back to 0 and the slot is available the next time FindFreeSlot runs.
Memory cost stays flat because the pool sits at full size from the start — no surprises mid-game. There’s no per-frame allocation, just a flag and a few floats, which is exactly what a 60 fps loop can afford. And the aactive scheme drops onto any pool you can think of: asteroids, player shots, UFO shots, and particles in my game all use their own pools with the same logic. For short-lived game objects in MMBasic the pool pattern isn’t one of several options, it’s effectively the only sensible one.