Tutorial:Adding_an_achievement_system - hpgDesigns/hpgdesigns-dev.io GitHub Wiki

In this tutorial, I'll show you how to add an achievement system to a game in Enigma.

Prerequisites

My guitar hero clone. Well, you can use any game, I'll use mine though as an example. Also, delete obj_score, it causes issues, and I draw the score with obj_emitter and such later.

Starting off

If your using my game, open it up in LGM and familiarize yourself with it first. What achievements are there to add? Lets add an achievement for hitting 5 notes in a row. Open up your controller object (An Object that does stuff like check if your dead or keep track of score. If you don't have one, make one and put it in your level), in Enigma Hero the controller object is obj_noteemit. Go to the create event, adding the event if you lack it, and drag in a code block. Type in:

bool achieved = 0;
string achievename = 'C-C-COMBO BREAKER';
consecutivehits = 0;

Here we define three variables. First is whether or not the achievement has been achieved, and the second is the name of the achievement. The third is 'consecutive hits'. If the player misses a note, we shall set this to zero. If the player gets a note, we shall add one to this, and if this value gets more then five, we will give them the achievement if they don't already have it.

Now, save and close the code window, and switch to normal step (If it isn't already there, make it). Drag in a code block (Before or after the already existing one, doesn't matter). In the code pop-up window, type:

if consecutivehits > 4 && !achieved then achieved = true;

Here, we check if there's more then 4 (5 is more then four) and if the player hasn't got the achievement yet. If so, we then give them the achievement.

Add/open the draw event and a code block, and in the code block type:

if achieved then {draw_text(0,0,"Achieved " + achievename);} else {draw_text(0,0,"Not Achieved" + achievename);}

This says achieved once its achieved. Ofcourse, this is just for testing purposes, we'll make nice fancy effects later.

Now, we must make consecutivehits go up. Open up obj_guitar, and goto the collision with obj_note event. Add in a new line underneath each line that says 'other.hit = true', and type in (Replacing obj_noteemit with your controller object, if your not using Enigma Hero)

obj_noteemit.consecutivehits +=2

We add two because of my cheater way of doing it. Next, open up the code in the Normal step event and add in this code in a newline below everytime it says score -= misspoints

obj_noteemit.consecutivehits -=1

Now, open up obj_note and in the outside of room event add in that same code below the 'score -= 2'(Within the code block). Run it and test. :D

Fancy Effects

Okay, so we now have a working achievement system, lets make it appear a tad better then how it currently does. Open up noteemit's create event, and in the code add

drawtimer = 0;

I'll explain this later. Anyway, open up the normal step event's code, and replace the 'if consecutivehits > 4...' line with

if consecutivehits > 4 && !achieved then {achieved = true; drawtimer = 200}

In the draw event, delete the current code and enter

draw_set_color(c_gray)
draw_rectangle(128,0,256,320,false);
draw_set_color(c_black)
draw_text(144,16,"Score: " + string(score))
if drawtimer > 0
{
if drawtimer > 100 {
draw_roundrect(128,0,256,64,8,false)
draw_set_color(c_white)
draw_text(128,0,achievename)
} else {
draw_blurredroundrect(128,0,256,64,8,100-drawtimer,drawtimer/100,false)
draw_set_color(c_white)
draw_text(128,0,achievename)
}
drawtimer -= 1
}

Now, save out of that. Next you'll need to add a custom script of mine, draw_blurredroundrect. Create a new script and name it draw_blurredroundrect, and put this inside of it:

x1 = argument0
y1 = argument1
x2 = argument2
y2 = argument3
r = argument4
blur = argument5
alpha = argument6
outline = argument7
iterations = 8;
draw_set_alpha(1/iterations*alpha)
xiter = iterations;
yiter = iterations;
while xiter > 0 {
    while yiter > 0 {
        xoffs = ((xiter/iterations)-0.5)*blur;
        yoffs = ((yiter/iterations)-0.5)*blur;
        draw_roundrect(x1 - xoffs, y1 - yoffs, x2 + xoffs, y2 + yoffs, r, outline);
        yiter -= 1;
    }
    xiter -= 1;
}
draw_set_alpha(1)

Test your game, and you should now get a fancy (Somewhat broken) blurring effect.

Adding more achievements

The title of this page is 'Adding an achievement SYSTEM'. That means more then one achievement. So, lets add another two.

Firstly, open up obj_noteemit's create event's code. Replace it with this:

bool achieved[3];
string achievename[3];
achievename[1] = "Over 0.09 THOUSAND"
achievename[2] = "Alest You suck les#then the creator"
achievename[3] = "C-C-COMBO BREAKER"
curachievename = "Error";
consecutivehits = 0;

What this does is it makes achieved and achievename into [Arrays]. Arrays basically allow you to store multiple values in the one variable. Since were adding two more achievements, we'll have three achievements in total. Next, go into its Normal step and replace the contents of the consecutivebits... block with

if score > 90 && !achieved[1] then {achieved[1] = true; drawtimer = 300; curachievename = achievename[1]}
if score > 5 && !achieved[2] then {achieved[2] = true; drawtimer = 300; curachievename = achievename[1]}
if consecutivehits > 4 && !achieved[3] then {achieved[3] = true; drawtimer = 300; curachievename = achievename[1]}