Auto-sprites
Home Site home Platforming Auto-sprites Vectors Path Finding quaternions.htm Variable Scope Water Panic Super Bouncy Ball Balls of Doom Contents k8055 GM Downloads

 

GM Studio
GM 8.1

Automatic sprite group assignment

There are two ways to implement this depending on which generation of Gamemaker is being used

Assignment in GM Studio

GM Studio has a nice look-up command to find a resource given its name as a string. It doesn't just work for sprites but can find other resources such as sounds and scripts too.

Assignment in GM 8.1 (and earlier)

GM 8.1 does not have a look-up command but it does have an "evaluate" function that can be used. Evaluating strings does bring some other problems with variable scope though.

Why I want to assign groups of sprites

There are times when it would be useful to be able to assign a group of sprites to an object, rather than just one. Currently if an object is to use multiple sprites then the sprites need to be listed in the event code.

Some flexibility can be obtained by putting the sprite IDs into variables in the create event, so at least only one bit of code needs editing, but it doesn't allow us to simply swap the sprite from the object properties box.

The technique I am advocating for GM8.1 and GM Studio is to use a system of sprite names to enable an object to "discover" its sprites based on the name of the assigned sprite. This allows you to set one initial sprite in the object editor and then code in the "create" event looks up the remaining sprite IDs.

It is possible that the technique could be extended to other resources, for example selecting sound effects based on the sprite name.

A refinement I have not yet looked into is that the sprite colour can be set in the room editor. If you are not using this to colour sprites then it could be used as a further index in the create event then reset to white afterwards. This would allow the same object to be "painted" to different appearances in the room editor.

Another trick might be to check for a different x or y scale and make decisions accordingly.

Drawbacks

The drawback to my scheme is it relies on the sprite names being accessible at run time It is likely that compiled versions of a GMS game will have the names stripped out since they are strings that are not normally needed for program execution, so a game using name-based sprite assignment would need alteration prior to compiling.

An attractive alternative scheme I would like to see added to GMS is for sprites in a group to have ascending sprite IDs, allowing an object to find its sprite set simply by adding numeric offsets to a sprite ID. I do not believe this is possible in Studio at the present time, though I haven't tested it. It might require the sprites to be created in a specific order.