If you write
function test:
local sp1=gensprite()
local sp2=gensprite()
loadsprite "sp1.png",sp1
loadsprite "sp2.png",sp2
endfuntion
gensprite() always returns the same number for both gensprite.
If you want it to work you have to do:
function test:
local sp1=gensprite()
loadsprite "sp1.png",sp1
local sp2=gensprote()
loadsprite "sp2.png",sp2
endfunction
Is this a "feature" or a bug?
(I like to declare all variables at the start...)
Just my take on this, but using gensprite would pick the first available "slot", if you dont fill that slot then the next time you use gensprite it would return the same index, until that spot is filled.
Just like Minion said.
I use a custom function (as suggested by someone? (Gernot?)) that steps through all sprites until it finds one without any dimensions.
I think maybe GENSPRITE doesn't reuse unloaded sprite IDs? (If it does, I can't see why I would use this function!)
Can't remember how to free a sprite, maybe 'LOADSPRITE "", sprite_id'?
// Find free sprite slot
FUNCTION Sprite_GetNextId%:
LOCAL id% = 1
LOCAL sx%, sy%
GETSPRITESIZE id, sx, sy
WHILE sx>0 OR sy>0
INC id
IF id > 30000 THEN RETURN 1 // If something goes wrong, overwrite sprite id 1. Could return '-1' instead, but would need to check value when returned
GETSPRITESIZE id, sx, sy
WEND
RETURN id
ENDFUNCTION
GENSPRITE does exaclty what you did in your function. Just in reverse order - it tries to find the largest free number available (so it's less possible to mess up older code). Same for GENSOUND, GENFONT.
except there is really some bug in GENSPRITE() i found today.
If I remove a image with LOADSPRITE "", then GENSPRITE can break and can pickup a number, there is allready in use (etc its replaced a image which its should not do that).
So I think I need that nice function above, and its works really nicely and workaround a unknown GENSPRITE() bug (as I not sure what it happens, but its after a LOADSPRITE "").
Can't say I get that here. However, a LOADSPRITE "" does generate the "error : No file" message in the status area
I removbed that error message for the next release.