Just so you know, arrays are global by default. So in my example above, if you had an array of similar objects, then rather than passing it into the function, pass it's array index instead.
Rather than using GOTO statements, use
exit to escape out of a loop.
Change this:
for enemy= 202 to 301
if object exist(enemy)= 0 then monsternumber= enemy: goto nextstep
next enemy
nextstep:
To this:
for enemy= 202 to 301
if object exist(enemy)= 0 then monsternumber= enemy: exit
next enemy
I would also suggest using constants in place of the 202 and 301 values.
Arrays are also expandable, like the Collections framework in Java.
Here's a different method for managing your monster creation:
#CONSTANT ENEMY_CLONE = 201
dim monsters() as Monster
function createMonster()
freeObject = getNextObjectNumber()
if freeObject
array insert at bottom monsters()
monsters().obj = freeObject
monsters().x = -200 + rnd(400)
monsters().y = -15
monsters().z = -200 + rnd(400)
clone object freeObject, ENEMY_CLONE
position object freeObject, monsters().x, monsters().y, monsters().z
endif
endfunction
function getNextObjectNumber()
for i = 1 to 10000
if object exist(i) = 0 then exitfunction i
next i
endfunction 0
First, we grab the next available object number. Then we insert a new element at the end of the array. (if we did it at the beginning, all existing elements would need to be shifted down) You'll notice I didn't specify an index, and in this case we don't need to. After inserting a new element, an internal pointer points at that new index automatically.
"You're all wrong. You're all idiots." ~Fluffy Rabbit