gensprite()

Previous topic - Next topic

ampos

If you write

Code (glbasic) Select
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:

Code (glbasic) Select
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...)
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Minion

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.

Slydog

#2
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'?

Code (glbasic) Select
// 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

My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Kitty Hello

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.

spacefractal

#4
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 "").
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

MrTAToad

Can't say I get that here.  However, a LOADSPRITE "" does generate the "error : No file" message in the status area

Kitty Hello

I removbed that error message for the next release.