INTRODUCTION
Hi all,
I see a lot of different ways to handle experience and levelling up, but I usually see they have some issues. Normally these issues are not obvious, and may never cause the developer trouble, but there is a good chance they will.
So my aim with this small tutorial is not how to make an interesting or complex levelling system, but how to create a very solid system that is easy to build on and twist in the future.
I myself love building RPGs so have tried many different ways to do this, and this is the most solid way to do it I have come across. But to note it here: if you have the paid version, use families! For both characters and arrays. Setting the variables in this tutorial to the families instead of the individual objects will make life much, much easier.
GETTING STARTED
First thing to do: create an array object. Then, it is good practice to rename it. I will call it 'experience'.
Now, create a player sprite and rename him 'hero'. Then give him the instance variable 'name', and make sure it is set to 'text' (NOT number or boolean). And then in that variable, add his name. I have called mine 'JohnBot'.
Now, you want to add some instance variables to that array!
- name
- level
- experience
- nextLevel
Also, add the 'Function' object to your project. There are ways to do it without using functions, but they are by far the best way, so it's a good idea to get used to them.
Lastly, create a new sprite called 'enemy'. He is not important, he will just be used in an example.
THE EVENTS
Okay, now we're set up and it's time to make some events!
First of all, we should find a way to link the array to the relevent character. So we do this:
This will make sure that an array is created when a new character comes along and will assign it to have the same 'name' variable as that character. This is a simple set-and-forget way to do it, and can be done for multiple characters.
Next, a simple event that uses a function to add experience.
Now you will notice the above event calls a function to add the experience. It uses 2 paremeters: the first parameter is the name of the character. The second parameter is the amount of experience to give that character.
Now, we need to make an event that uses that function name!
So as you can see, it will pick the correct array by checking its 'name' instance variable to make sure it matches that of the first function parameter. Then, it will add the function's second parameter to the array's 'experience' instance variable.
Now to finish up, we want the experience to do something.
There we go! Now when the experience gets to an amount greater than the 'nextLevel' instance variable, the character will level up! It will also make the next level a little harder to reach by increasing the value of 'nextLevel'.
WRAPPING UP
And there you have it, a very solid base to creating a levelling system. While it looks simple, you can really evolve it into anything you want in the future. And you can do it without having to start with a bulky engine or set any mechanics in stone. The other advantages of this system:
- Arrays are not destroyed between layouts or when characters are destroyed, therefore are good candidates for holding these variables.
- Unlike dictionaries, arrays can do a lot of tricks with their cells. While they are not used in this tutorial, if you later decide you want to do some complex stuff, this way will save you a lot of picking and referencing.
- Using global variables to track this stuff is messy, especially if you end up having many characters who level up! Also for this system you can apply it to temporary characters if you like, so you could have hundreds of characters levelling in your game without bloating your variables.
- Using functions to add the experience is much better than eventing it! Even if all your numbers are perfect, what happens if you all of a sudden realise you want to give a character some HP when they gain experience? Now you have to go back to every single event that adds experience and add that new action! Not a position you want to be in. This way, you just change ONE event! Not only is it faster, but also safer. You won't run into an issue where you mistype one of those actions or forget one altogether.
- By naming the array's 'name' variable, it's very easy and intuitive to reference and manipulate them. You may however want to use UIDs instead, depending on how you set up your game. I would recommend UIDs for anything that uses temporary characters, but if they are all staple it's easier to just use names.
So there you go, hope it helps someone! Feel free to ask questions or give suggestions for alternatives.
And very lastly, as requested, an updated capx! This capx is an advanced version of this tutorial, using families and actually giving you the ability to destroy some enemies so you can see experience allocation in action. Let me know how ya'll get on!