This is my version of the popular 'free object' snippet, except you can choose a range of numbers you want your object to be created in.
In case you didnt know, this snippet goes through a range of numbers (specified by you) and finds the next free (unused) object available.
It is easily modified for sounds or images, ect.
Enjoy!
Language:
Dark Basic Pro
Code:
obj = freeobj(100,200) make object cube obj,20 wait key end function freeobj(start,finish) for i = start to finish if object exist(i) = 0 freeobj = i i = finish+1 endif next i endfunction freeobj
(1 vote)
Wed, 08/29/2007 - 08:35
I've not used DBP in a few weeks so I'm a little rusty, but I recall there being an "exitfunction" which you could use here.
When you're looping over the object ID's, once a match is made you should be able to do "
exitfunction i" as that will return whatever i is at that point.One slight issue I have with this function is what happens when the entire range of numbers is used - the result would be the last ID you specified (in the above case, 200) would be returned even though its used. I suppose the answer here is to do 1 last check on the value returned to make sure its definitely free...
Nice function though!