Search The Glog

Saturday, August 17, 2013

Awards system for Gamemaker games

This is the awards (achievement) system I used in my recent games, Trapped and PushIt. It is very simple, as it uses only one object, one scripts, one global variable, and can show multiple achievements, as shown in the picture below.

Awards system of GameMaker

First you need to create a global variable at the game initialization store the awards, global.awards='0000000000'. It uses 10 zeros as in this example there are 10 awards. Also we need the sc_award_text(award) script to set the awards text. The script has one argument, the award number (0...10):
var text='';
switch (argument0) {
case 1: text='First award';break;
case 2: text='Another award';break;
case 3: text='Yet another award';break;
case 4: text='Fourth award';break;
case 5: text='And so on...';break;
case 6: text='Test 6';break;
case 7: text='Test 7';break
case 8: text='Test 8';break;
case 9: text='Test 9';break;
case 10: text='Test 10';break; }
return text;

Next, we create the caller event. It basically checks if an awards in achieved and is still unlocked, and if these conditions are met,  creates the ob_awards object, and sets it's award variable to the number of the desired award. The code for achieving the 2nd award is:

if string_char_at(global.awards,2)=='0' {
global.awards=string_delete(global.awards,2,1);
global.awards=string_insert('1',global.awards,2);
showAward=instance_create(0,0,ob_award);
showAward.award=2;
}
To use the code for different awards just replace '2' with any number from 1 to 10.


Next we create the main object for showing awards: ob_award. In the Create event we have the following code:
multiple=instance_number(ob_award);
awardHeight=45;
awardWidth=150;
text='';
award=0;
x=room_width;
y=room_height-awardHeight;
hspeed=-10;
As you can see, first it checks if multiple awards are displayed on screen, and set the variable multiple to the number of awards. Next is sets the width and height of the award rectangle, and the initial text and award to zero, because these are later set to the corresponding values, according to the achieved award. Lastly it sets the x, y and hspeed so the award moves from the bottom left to bottom middle.

In the Step event we have the code:
text=sc_award_text(award);
if x>room_width+1 instance_destroy();
if (hspeed<0 and x==room_width-awardWidth*multiple) {
hspeed=0;
alarm[0]=50;
audio_play_sound(snd_award,1,0);
}
First it sets the variable text to the desired text to be displayed (eg. 10000 points), calling the sc_award_text(award) script discused earlier. Next it checks if the award moves outside the room, and if it is destroys it. Lastly, checks if the award has reached the bottom middle of the screen. If so, stops any movement, and sets the alarm[0]=50. This determines how long the awards will be shown on screen.

Next, in Alarm[0] event just put hspeed=10; so the award goes back to the right.

Finally, we need to draw the achievement, in the Draw event:
draw_set_color(c_black);
draw_set_alpha(0.7);
draw_roundrect(x,y,x+awardWidth-10,y+awardHeight-5,0);
draw_set_alpha(1);
draw_set_font(font_award);
draw_set_color(c_aqua);
draw_set_halign(fa_center);
draw_text(x+70,y,'Award unlocked:');
draw_set_color(c_yellow);
draw_text(x+70,y+20,text);
As a bonus, if you want to show the awards, you will need one more script and one more object. The script checks if an award is unlocked or not, using argument0 as the award to be checked. If it is unlocked set the drawing color to c_navy and returns true, else set the color to c_gray and returns false. The sc_awardUnlocked(award) script is:
if (string_char_at(global.awards,argument0)=='1') {
draw_set_color(c_navy);
return true;
} else {
draw_set_color(c_gray);
return false;
}
To show the award list, create a sprite sp_award with one shiny medal on frame 0, and gray medal on frame 1. Then create an object and put in the Draw event:
draw_set_font(font_award);
draw_set_color(c_black);
draw_set_halign(fa_left);
for (i=1;i<11;i+=1) {
if sc_awardUnlocked(i) draw_sprite(sp_award,0,x,y+i*32)
else draw_sprite(sp_award,1,x,y+i*32)
draw_text(x+40,y+i*32,sc_award_text(i));
That'all. Here you can see an example of the tutorial in action !