Level/XP System in GameMaker: Studio

In this little tutorial you’re going to learn to make a basic leveling system where you level up by collecting experience points (or exp, or xp). Adding a leveling system to a game adds a certain kind of depth to it and makes quests more interesting. Also, it’s an essential part to creating an RPG. So, without further ado, let’s take a look at how it’s done!


The Variables

First of all, we need to make global variables that can store the xp and level. Let’s make an object called obj_backend which will serve as a background and persistent object that stores the global variables. After creating the object, initiate the global variables in its Create event:

globalvar level, xp;
level = 1;
xp = 0;

In its Step event, add:

if (xp >= level * 100){
 xp = xp - level * 100;
 level++;
}

This will increase the level by one when the xp reaches the amount level * 100. The level value is multiplied by 100 so that with each increasing level, it would require the player more xp to further increase the level. The xp is reduced to what is left after leveling up.

Testing

Let’s test this code in a test project. Here I have an object that increases xp when you collect it. It just uses this simple code in its Collision with player event:

xp += 10;
instance_destroy();

So as you see, it would increase the xp by 10 before being destroyed.

Just for testing, I used this code in the Draw event of obj_backend to see the level and xp:

draw_text(0, 0, "LEVEL: " + string(level));
draw_text(0, 32, "XP: " + string(xp));

1.gif

You can see how the xp keeps on increasing and as it reaches the value level * 100, the level increases and the xp drops back to 0.

XP bar

Now we’ll create an XP bar, which will be pretty much like a healthbar but one that shows xp. Most RPGs show this bar when a battle ends, and that is when the accumulated points from the battle are added to the xp. For this, we need another global variable called points, which stores the accumulated points collected during the game, which will then be added to the xp.

So let’s initiate the variable in the Create event of obj_backend:

globalvar points;
points = 0;

And I’ll also change the behavior of the xp increasing objects to increase the points instead of the xp.

points += 10;
instance_destroy();

Let’s now get to the main part.

We’ll create a new object called obj_levelshow which shows the increase in level and xp at the end of the game/level/battle phase.

In its Create event, we’ll initiate two new variables:

xp_show = xp;
level_show = level;

xp += points;
points = 0;

The xp_show and level_show variables are the variables that will show the increase in xp and level.

Then the points are added to xp before dropping it to 0.

In its Step event, we’ll make some algorithm to show the increase in xp and level through xp_show and level_show:

if (level == level_show){
 if (xp > xp_show){
 xp_show += 1;
 }
}
else if (level > level_show){
 if (xp_show < level_show * 100){
 xp_show += 1;
 }
 else{
 level_show++;
 xp_show = 0;
 }
}

Here, if the level is the same (meaning it hasn’t increased) and just the xp has increased, it will show that increase. But if the level has increased, it will increase the xp all the way up until it shows an increase in the level.

In its Draw event we will tell it to show the level and the XP bar:

draw_text(x, y, "Level: " + string(level_show));
draw_healthbar(x, y + 20, x + 50, y + 30, (xp_show/(level_show * 100))*100, c_black, c_green, c_green, 0, true, true);

The expression (xp_show/(level_show * 100))*100 is used to calculate the percentage used to draw the bar.

Time to test!

I’ve placed an object that creates this obj_levelshow as if the level has been completed, in its Collision with player event:

instance_create(400, 300, obj_levelshow);
instance_destroy();

2.gif

You can see that it works great. However, the more level you achieve, the slower the XP bar increases. To fix that, open your obj_levelshow, and replace its Step event with this:

if (level == level_show){
 if (xp > xp_show){
 xp_show += 1 * level_show;
 }
}
else if (level > level_show){
 if (xp_show < level_show * 100){
 xp_show += 1 * level_show;
 }
 else{
 level_show++;
 xp_show = 0;
 }
}

The changes are highlighted in bold text.

Conclusion

So now you have your own leveling system! You can modify this to your liking and add more features.

You can now use the level variable to unlock items or pathways in your game, or to do more interesting stuff!


Click here to download this project

Want to know how to make a save/load system to save your progress in the game? Read here.

Need more help, or want to ask something? Use the comment section below or mail me at gurpreetsingh793@gmail.com. If it helped you out, why not share it with others and help them out too? 😉 Also, scroll down to the bottom of this page and press Follow to stay updated on new tutorials.

Join your fellow GameMakers on GameMaker’s Guild (Facebook) and on the GameMaker Community (YoYoGames forum).

Thanks for reading, and have a great day!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s