The first thing I found annoying in Dark Basic was the way objects, media etc need to be assigned a number. This code snippet works very well to solve that.
Sample Usage:
` I use obj as a naming prefix - the variable is an integer
objMyAirplane = GetNewID(cnObject)
MAKE OBJECT SPHERE objMyAirplane, 20
` Same technique for all the types
mtxMyMatrix = GetNewID(cnMatrix)
MAKE MATRIX mtxMyMatrix, 1000,1000,100,100
musMyMusic = GetNewID(cnMusic)
LOAD MUSIC "MyMusic.mid", musMyMusic
NOTE: (DBPro - Sparky DLL Users) Sparky Says no Mesh 255, and I think something else also - so slight mod to this code will make Sparky Safe! Just Make Mesh Counter and the other one??? (I forget) Skip 255.
Source Code:
`---------------------------------------------------------------------
` Related to GetNewID() Function and more in the "Things" Subsystem
`---------------------------------------------------------------------
#CONSTANT cnObject =1
#CONSTANT cnCamera =2
#CONSTANT cnMatrix =3
#CONSTANT cnBitmap =4
#CONSTANT cnImage =5
#CONSTANT cnLight =6
#CONSTANT cnMusic =7
#CONSTANT cnAnimation =8
#CONSTANT cnSound =9
#CONSTANT cnMemBlock = 10
#CONSTANT cnTerrain=11
` -----------------------------------------------------------------------------------------------------------------------
` Note this works great but I could see it slowing down If you had ALOT of objects of a particular Type
` and trying to Add another of the Type. That's what the GetNewID_StartAt function is all about. If you KNOW,
` For example you have a 1000 items in a scene, and Now need to be able to Add and Remove the bad guys
` QUICKLY during the game, use this routine - and specify a STARTHERE value that is greater than your highest object,
` or just pick a number out in Left field. Alternatively, If you wanted to reserve #'s 1-100, say for bad guy OBJECTs,
` Call all your other creation like: GetNewID_StartAt(cnObject,101) - so your reserved 1-100 is Not used.
Function GetNewID(p_iType)
` -----------------------------------------------------------------------------------------------------------------------
iNewId = GetNewID_StartAt(p_iType, 1)
ENDFUNCTION iNewID
` -----------------------------------------------------------------------------------------------------------------------
` -----------------------------------------------------------------------------------------------------------------------
` Same As GetNewID(p_itype) With control of WHERE the Function starts seeking out an am empty slot.
` NOTE: To Wrap Around Logic is in Place. Searching Stops If a free "thing" is Not available by the Time the
` counter (iSeeker) hits 65535. (Hardcoded)
Function GetNewID_StartAt(p_iType, p_iStartHere)
` -----------------------------------------------------------------------------------------------------------------------
iSeeker=p_iStartHere
iNewID =0
repeat
inc iSeeker
If p_iType=cnObject
If OBJECT EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnBitmap
If BITMAP EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnCamera
If camera EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnTerrain
If TERRAIN EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnSound
If Sound EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnMusic
If MUSIC EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnImage
If image EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnLight
If light EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnAnimation
If Animation EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
If p_iType=cnMemBlock
If Animation EXIST(iSeeker)=False
iNewID = iSeeker
ENDIF
endif
until (iNewID<>0) or (iSeeker>=65535)
ENDFUNCTION iNewID
` -----------------------------------------------------------------------------------------------------------------------
Have Fun!
Instructions: Place the Source code in your project. Place the Constants in the top of your application so they get "initialized". Then Call the GetNewID or the alternative GetNewID_StartAt function whenever you need a new ID# to use without fear of using a number in use somewhere else in your program.
Note - due to the simple nature of this function - and how it checks if the object exists first before returning you a new id, you can integrate it into existing programs you have written usually without to much hassle - allowing you to add new things without having to remember what the other ID's are.
IMPORTANT: Also due to the nature of how this function get's the ID's - You need to USE the ID you're given as soon as you get it like in the sample usage codesnippet - otherwise you might get the same ID twice because that ID is not yet "in use"
See Ya!
Comments welcome.
Jason
Know way to many languages - Master of none