Author Topic: Problem using GENSPRITE()  (Read 3534 times)

Offline Bursar

  • Mr. Drawsprite
  • **
  • Posts: 63
    • View Profile
Problem using GENSPRITE()
« on: 2010-Sep-28 »
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.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Problem using GENSPRITE()
« Reply #1 on: 2010-Sep-28 »
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]

Offline Bursar

  • Mr. Drawsprite
  • **
  • Posts: 63
    • View Profile
Re: Problem using GENSPRITE()
« Reply #2 on: 2010-Sep-28 »
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

  • Guest
Re: Problem using GENSPRITE()
« Reply #3 on: 2010-Sep-28 »
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]

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Problem using GENSPRITE()
« Reply #4 on: 2010-Sep-28 »
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

  • Guest
Re: Problem using GENSPRITE()
« Reply #5 on: 2010-Sep-28 »
Yes, pathing is very important - GENSPRITE returns the next free slot.  If a sprite can't be loaded, then the value wont be increased...

Offline Bursar

  • Mr. Drawsprite
  • **
  • Posts: 63
    • View Profile
Re: Problem using GENSPRITE()
« Reply #6 on: 2010-Sep-28 »
Ah yes, the pathing thing fixes it. Thanks :)

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

  • Guest
Re: Problem using GENSPRITE()
« Reply #7 on: 2010-Sep-28 »
Yes, it does need changing.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10732
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Problem using GENSPRITE()
« Reply #8 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.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: Problem using GENSPRITE()
« Reply #9 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

  • Guest
Re: Problem using GENSPRITE()
« Reply #10 on: 2010-Sep-30 »
You could before - although if you aren't careful you'll end up with messy code.