Problem using GENSPRITE()

Previous topic - Next topic

Bursar

I'm trying to use GENSPRITE() to make assigning numbers to sprites easier, but it just crashes my game.

If I do this:
Code (glbasic) Select

GLOBAL menuPointerSprite = GENSPRITE()
LOADSPRITE "/Menu/pointer.png", menuPointerSprite

As indicated in the help file, the game compiles correctly, but when it runs I just get the spiny circle icon, and my app never appears. I have to go into Task Manager and kill it off.

I've also tried this:
Code (glbasic) Select

GLOBAL menuPointerSprite = 0
menuPointerSprite = GENSPRITE()
LOADSPRITE "/Menu/pointer.png", menuPointerSprite

My game compiles and runs, but it always assigns 0 to menuPointerSprite.

For instance:
Code (glbasic) Select

GLOBAL screenGrab = 0
screenGrab = GENSPRITE()

GLOBAL menuPointerSprite = 0
menuPointerSprite = GENSPRITE()
LOADSPRITE "/Menu/pointer.png", menuPointerSprite

PRINT screenGrab, 10,10
PRINT menuPointerSprite, 10,20

Shows me that both screenGrab and menuPointerSprite are equal to 0. I don't think I'm doing anything wrong (famous last words!)...

This is running under Windows 7 Ultimate, 64bit.

Slydog

I don't think sprites are known by GENSPRITE() until they are actually allocated, such as using the LOADSPRITE() command.  So both your GENSPRITE() commands return '0' because at the time you called them, no sprites where ever allocated.

So in your example, try:
Code (glbasic) Select
   GLOBAL screenGrab = 0
   screenGrab = GENSPRITE()
   LOADSPRITE "/Screen/grab.png", screenGrab    // <- Must load sprite before calling next GENSPRITE()!!!
   
   GLOBAL menuPointerSprite = 0
   menuPointerSprite = GENSPRITE()
   LOADSPRITE "/Menu/pointer.png", menuPointerSprite

   PRINT screenGrab, 10,10
   PRINT menuPointerSprite, 10,20
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Bursar

Nope, doesn't work. Both still print 0.

In fact, if I do this:
Code (glbasic) Select

GLOBAL screenGrab = 1
screenGrab = GENSPRITE()
LOADSPRITE "/Menu/outer.png", screenGrab

GLOBAL menuPointerSprite = 2
menuPointerSprite = GENSPRITE()
LOADSPRITE "/Menu/pointer.png", menuPointerSprite

PRINT screenGrab, 10,10
PRINT menuPointerSprite, 10,20


It still prints 0 for both sprite numbers, so GENSPRITE isn't actually working.

MrTAToad

There is no problem with GENSPRITE.  Unfortunately, there is with your pathing.

Try this instead :

Code (glbasic) Select
GLOBAL screenGrab = 1
screenGrab = GENSPRITE()
LOADSPRITE "./Menu/outer.png", screenGrab

GLOBAL menuPointerSprite = 2
menuPointerSprite = GENSPRITE()
LOADSPRITE "./Menu/pointer.png", menuPointerSprite


Oh, and the only command that can initialise variables is RGB

[attachment deleted by admin]

Slydog

Then perhaps those paths to your .png files are wrong?
Did you try setting the current directory?:
Code (glbasic) Select
SETCURRENTDIR("Media")
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

Yes, pathing is very important - GENSPRITE returns the next free slot.  If a sprite can't be loaded, then the value wont be increased...

Bursar

Ah yes, the pathing thing fixes it. Thanks :)

Quote from: MrTAToad on 2010-Sep-28
Oh, and the only command that can initialise variables is RGB
That's a change which needs to be made to the help file then, as it states:
Code (glbasic) Select

GLOBAL gImgFoo = GENSPRITE(); LOADSPRITE "foo.png", gImgFoo
GLOBAL gImgBar = GENSPRITE(); LOADSPRITE "bar.png", gImgBar
DRAWSPRITE gImgFoo,0,0
DRAWSPRITE gImgBar,0,100
SHOWSCREEN
MOUSEWAIT

MrTAToad

Yes, it does need changing.

Kitty Hello

I fixed that GLOBAL a=foo() for the next update. You can use _ANY_ functions now. The initialization is called right before __MainGameSub kicks in.

Slydog

Quote from: Kitty Hello on 2010-Sep-30
I fixed that GLOBAL a=foo() for the next update. You can use _ANY_ functions now. The initialization is called right before __MainGameSub kicks in.
Wow, that's great news!

Will this let us call functions inside other function calls, such as:
Code (glbasic) Select
LOCAL v1 as TVector
v1 = VectorAdd( VectorNew(1, 2, 3), VectorNew(4, 5, 6) )
Or could we before and I just didn't realize it?!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

MrTAToad

You could before - although if you aren't careful you'll end up with messy code.