Hello Guest

Author Topic: Nitpick bug on GetSpriteIdByName()  (Read 4233 times)

mangulator

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Nitpick bug on GetSpriteIdByName()
« on: February 06, 2013, 10:50:38 pm »
I recently had to index a bunch of sprites by id. After looking at the docs, I added an error check for id zero, assuming that meant the named sprite could not be found. But I actually have a sprite zero in all my collections (makes sense). Perhaps returning -1 to indicate failure would be more useful?

Thanks Unikron for a great library.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Nitpick bug on GetSpriteIdByName()
« Reply #1 on: February 06, 2013, 10:55:33 pm »
There is a second undocumented variant of that function which takes a "default" value. I'm not sure if its in 1.90 but definitely in 1.91, first beta of which will be out very soon.

So -
Code: [Select]
GetSpriteIdByName("name", -1) will return -1 if not found.
Also, in 1.91 there will be the following other useful functions:
Code: [Select]
sprite.SetSprite( spriteId );
sprite.SetSprite( "spriteName" ); // returns false if sprite not found, doesn't do anything to the sprite
and...
Code: [Select]
sprite.SetSprite( newCollection, spriteIdFromNewCollection );
sprite.SetSprite( newCollection, "spriteNameFromNewCollection" );