Referencing sprites dynamically

Schwee

Member
Anyone know the proper way to do this? I have multiple characters that all have the same animations, and it would be great to have my obj_member object to just have a 'name' property and is able to fetch the sprites from there.

Trying something like: sprite_index = "spr_" + name + "_idle";

This is not working, throws an error 'unable to convert string "spr_matt_idle" to integer'.
 

Schwee

Member
Nevermind...solved. I googled this before posting but I tried again with the exact title I made here and found some better material lol.

For anyone who is curious:

Code:
sprite_index = asset_get_index( "spr_" + name + "_idle");
 
H

Homunculus

Guest
You can do that using asset_get_index . If the name attribute is know at creation, you may consider pre-generating the sprite index instead of calling the function every time you are in a different state, something like

Code:
sprite_idle = asset_get_index( "spr_" + name + "_idle");
sprite_running = asset_get_index( "spr_" + name + "_running");
//etc...
 

TheouAegis

Member
Disclaimer: GMs2 can change internal construct at any time. This method is perfectly acceptable in GMS1, though:

sprite_index = spr_matt_idle + selected

Where selected is the enumerated value of the selected character/outfit/whatever. All resources are automatically assigned numerical IDs based on their positions in the resource tree in Studio.
 
Top