A. Sprite number does not necessarily have to be a global. The variable type doesn't matter, only the value.
You can create sprites in any order you would like. So you could create sprite 504 and then sprite 12 and then sprite 134 if you need to. But you need to make sure the sprite number is available, and is not already in use by another sprite.
There are some great functions for this in the matrix1utils plugin which you can download from this forum.
Here's another option:
REM - Finds an available sprite resource number.
function GetFreeSprite()
spr as integer
spr = 0
Found = 0
repeat
s = rnd(21999999) + 1
if sprite exist(s) = 0
spr = s
Found = 1
endif
until Found
endfunction spr
Here's an example of how one could create items:
REM - Constants for ItemTypes.
#constant ITEMTYPE_WEAPON 1
#constant ITEMTYPE_HEALTHPOT 2
REM - Main list UDT.
type ITEM_MAIN_LIST
itemtype as integer
itemid as integer REM - Index on the specific lists.
endtype
REM - Weapon type UDT.
type ITEM_WEAPON_TYPE
name as string
spr as integer
img as integer
strength as integer
endtype
REM - HealthPotion type UDT.
type ITEM_HEALTHPOT_TYPE
spr as integer
img as integer
health as integer
endtype
REM - Initialize ITEM globals and dynamic arrays.
function ITEM_Init()
global dim ITEM_List() as ITEM_MAIN_LIST
global dim ITEM_Weapon() as ITEM_WEAPON_TYPE
global dim ITEM_HealthPot() as ITEM_HEALTHPOT_TYPE
global ITEM_ListSize as integer
global ITEM_NumWeapons as integer
global ITEM_NumHealthPots as integer
ITEM_ListSize = 0
ITEM_NumWeapons = 0
ITEM_NumHealthPots = 0
endfunction
REM - Terminate ITEM functions.
function ITEM_Terminate()
ITEM_DeleteAllItems()
undim ITEM_HealthPot()
undim ITEM_Weapon()
undim ITEM_List()
endfunction
REM - Adds a new item of a certain type, and inserts it on its specific list.
REM - Also inserts it into the main list, linking it to the specific list.
function ITEM_NewItem(itemtype as integer)
Index as integer
if itemtype = ITEMTYPE_WEAPON
array insert at bottom ITEM_Weapon()
Index = ITEM_NumWeapons
inc ITEM_NumWeapons, 1
REM - Sets default values. Change in another function.
ITEM_Weapon().name = ""
ITEM_Weapon().spr = GetFreeSprite() REM - GetFreeSprite(), see other snippet...
ITEM_Weapon().img = 0
ITEM_Weapon().strength = 0
endif
if itemtype = ITEMTYPE_HEALTHPOT
array insert at bottom ITEM_HealthPot()
Index = ITEM_NumHealthPots
inc ITEM_NumHealthPots, 1
REM - Sets default values. Change in another function.
ITEM_HealthPot().spr = GetFreeSprite()
ITEM_HealthPot().img = 0
ITEM_HealthPot().health = 0
endif
REM - Insert into the main list.
array insert at bottom ITEM_List()
ITEM_List().itemtype = itemtype REM - What itemtype array to look in.
ITEM_List().itemid = Index REM - What index in the specific array to find it.
Index = ITEM_ListSize REM - Return index to item in main list...
inc ITEM_ListSize, 1 REM - We can find its specific info through that.
endfunction Index
REM - Delete an Item. (And the sprite it uses)
function ITEM_DeleteItem(Index as integer)
if Index >= 0 and Index < ITEM_ListSize
if ITEM_List(Index).itemtype = ITEMTYPE_WEAPON
i = ITEM_List(Index).itemid
if sprite exist(ITEM_Weapon(i).spr) then delete sprite ITEM_Weapon(i).spr
REM - Possibly delete image, but probably not since it's shared with other sprites.
array delete element ITEM_Weapon(), i
dec ITEM_NumWeapons, 1
endif
if ITEM_List(Index).itemtype = ITEMTYPE_HEALTHPOT
i = ITEM_List(Index).itemid
if sprite exist(ITEM_HealthPot(i).spr) then delete sprite ITEM_HealthPot(i).spr
REM - Possibly delete image, but probably not since it's shared with other sprites.
array delete element ITEM_HealthPot(), i
dec ITEM_NumHealthPots, 1
endif
array delete element ITEM_List(), Index
dec ITEM_ListSize, 1
endif
endfunction
REM - Deletes all items.
function ITEM_DeleteAllItems()
while ITEM_ListSize > 0
ITEM_DeleteItem(0)
endwhile
endfunction
Yeah... I got carried away...