I have no idea if this is how Steve (or Ian who suggested the idea to him) would implement this, but after reading the newsletter I thought I might give it a go.
Seems like it could be useful considering my last (and now current again) project got bogged down in keeping track of object and image numbers.
So you start off by creating your Unit/Enemy/Whatever udt and an array declared as that type.
Then create two more arrays to hold the 'available' and 'used' object numbers. The 'available' array has a size of the maximum number of objects possible in your game, and the 'used' array is emptied.
Whenever you want to create a new object you call the 'object_create()' function, this returns the new object number which is stored in your unit/enemy udt array.
When you are finished you just call the 'object_delete(object_number)' function.
REM Unit UDT
type unit
object as integer
xpos as integer
ypos as integer
endtype
REM Unit Array
dim unit(20) as unit
REM Object Arrays
dim OBJECT_available(max_objects)
for obj = 1 to max_objects
OBJECT_available(obj) = obj
next obj
dim OBJECT_used(0)
empty array OBJECT_used()
REM This bit goes in your main code when you want to create a new unit or other object
for u = 1 to 20
unit(u).object = object_create()
next u
REM This bit goes where you want to delete the object
for u = 1 to 20
object_delete(unit(u).object)
next u
REM Object Create Function
function object_create()
`Set array index to last entry in available object stack
array index to stack OBJECT_available()
`Create a new index in used object stack
add to stack OBJECT_used()
`Set new index value to that of the value at the current index of OBJECT_available (the last entry)
OBJECT_used() = OBJECT_available()
`Set this as the value to be returned by the function, the value that is stored in the unit array
new_object = OBJECT_used()
`Remove last index from OBJECT_available stack
remove from stack OBJECT_available()
endfunction new_object
REM Object Delete Function
function object_delete(object)
if object exist(object)
`Set array index to first in used object stack
array index to top OBJECT_used()
`While loop through all indices that exist in OBJECT_used stack
while array index valid(OBJECT_used())
`If this current index holds a value equal to that sent to the function...
if OBJECT_used() = object
`Delete this element (index) from the stack
array delete element OBJECT_used()
`Add a new index to the available stack
add to stack OBJECT_available()
`Set this new index to the value of the object number sent to the function
OBJECT_available() = object
else
`If the OBJECT_used array at this index does not hold the value of object, skip to next
next array index OBJECT_used()
endif
endwhile
endif
endfunction
Sorry this code can't be copy-pasted into DBP and run, but I hope it helps.
If anyone has any suggestions on improvements or different methods, please suggest them. I'm new to this technique.
As with most people I jump around which projects I work on and which I drop for a while.
Currently I'm back working on Sioux, a Hollywood Western RTS.