I can think of two different approaches, the earlier keeps code simple whilst requiring more graphics; the latter needs no additional sprites though a little more code to reference the correct sprites.
Solution 1: a specific sprite for characters
You add just one line of code, and use an additional sprite resource representing the characters preview to be drawn on your button.
If your buttons have an index reference to a given character like this:
/* Button Create Event */
char_ref = -1; // character reference
Create a sprite with as many frames as the number of your characters. Each frame depicts one of your characters (so, 5 char's = 5 frames). Let's call it sprButtonCharacters
.
If sprButtonCharacters
' first frame is the first character, and so do other frames, you can use the value of char_ref
to choose a sub-image of sprButtonCharacters
to be drawn on top of the button object.
In the button Draw Event:
/* Button Draw Event */
draw_self(); // draw sprite_index
draw_sprite(sprButtonCharacters, char_ref, x, y); // draw char. sprite
You are first drawing the button (you can change draw_self()
with whatever script or functions you use to render the button), then you draw the sprite representing the characters on top of it by choosing the desired sub-image.
You can add some values to x
and y
if you need to draw such sprite with given horizontal and vertical offsets.
Solution 2: everything via code
Every time you add a new character, just add a new line of code to a script in charge of choosing the sprite to be drawn onto the button. No additional sprites needed.
We still have a char_ref
variable which tells us what character the button is referencing to, but this time we have different code functions to draw the overlay sprites we need. In the button Draw Event:
// Button Draw Event
draw_self(); // draw sprite_index
switch (char_ref) // choose character sprite to draw
{
case 0: draw_sprite(sprTonyIdle, 0, x, y); break; // first character
case 1: draw_sprite(sprAllyCyborg, 0, x, y); break; // second character
case 2: draw_sprite(sprRogueWarrior, 0, x, y); break; // third character
{ ... }
default: break; // no character assigned
}
This way you can assign any sprite you want to be drawn by the button, and no new resources are needed since the sprite is supposed to be already present in your game project.
Also, if you use -1 instead of 0 as subimage
argument to sprite_draw()
, you can draw animated character previews instead of static ones.