Learning Kyra: Sprite Loading

While working on Oracle’s Eye, I learned something new about the Kyra Sprite Engine. For some time I’ve wondered how I would load sprites from a script. Normally, I would load a sprite resource with:

GetSpriteResource( Char_Player)

where Char_Player is a constant defined in a .h file generated by Kyra’s Sprite Editor. While I could write a file to load the U32 that the constant referred to, it seemed unwieldy to have a script file that looked like:

Player 1 < < 0
Enemy 2 < < 0

I would think that updating such a file would be confusing, if not a nightmare.

So while perusing the code files looking for information on how to do something else entirely, I saw that the test programs provided with Kyra had lines that looked like:

bigRes = engine->Vault()->GetSpriteResource( "LARGE" );
medRes = engine->Vault()->GetSpriteResource( "MED" );
smallRes = engine->Vault()->GetSpriteResource( "SMALL" );

I was excited that I could apparently load sprites by name, but I didn’t remember seeing anything about how to do it in the documentation. I looked more closely and found something about how to refer to a sprite’s action by name, but not the sprite itself. After some more hunting through the code, API, and documentation, I figured it out.

The name of the constant Character_Player was made by creating a sprite called Player. Character_ was just a prefix I used for all characters. Kyra allows me to refer to the name of the sprite as “Player”, however. So:

GetSpriteResource( Char_Player)
is interchangeable with
GetSpriteResource( "Player")

and naturally that means that I can do something like this:

string name = GetEntityName();
GetSpriteResource( name.c_str() );

Voila! Sprite loading by script is possible! The only thing I need to do is make sure that the names that I give my sprites using the Sprite Editor are the same as the names in my script, but it is definitely better than syncing the names with things like ( 1 << 0 ).