ID:292502
 
This tutorial will get us started on the creation of a simple leveling system by adding the variables and procedures that we'll need.
If you haven't already read Tutorial #2 we recommend that you check it out first. Or, start at the beginning, if you're new to the series!

Adding Variables
Variables are used to store data. They can hold numbers, text, lists, and even reference entire players or other objects.

Step 1.1 To get started, lets first create a new Code File in our Tutorial Environment named "Vars.dm"

Step 1.2 Add the following code:
mob/var
Level=1
Exp=0
Nexp=100

This creates 3 new variables for all /mob types in the game. This includes our mob/Player, because it is a child or sub type of /mob; /mob/Player.
"Level" will store the player's level, and starts out as 1
"Exp" is how much exp they currently have, which starts at 0
and "Nexp" is how much exp they need for their next level, in this case 100


Adding Procedures
In this next part we'll create our first procedure or "proc".
A proc is a lot like a verb (covered in the previous tutorial), except instead of being available to players, it can only be called directly through the code.

Step 2.1 Create a new Code File in our Tutorial Environment named "Procs.dm"

Step 2.2 Add the following code:
mob/proc
LevelCheck()
if(src.Exp>=src.Nexp)
src.Exp=0
src.Nexp+=10
src.Level+=1
src<<"You are now Level [src.Level]"

First, take note of the important fact that we use "src" instead of "usr".
src is what the code is being run on, and usr is what caused the code to run.
In some cases usr and src may be the same, but you should almost always use src instead of usr.

Next lets go over it line by line
"mob/proc/LevelCheck()" creates the new mob proc called LevelCheck
"if(src.Exp>=src.Nexp)" checks if the src's Exp is greater than or equal to their Nexp.
The remaining lines are tabbed under the if(), which means they'll only happen if the if() is true
"src.Exp=0" resets the src's Exp to 0 for their next level
"src.Nexp+=10" increases their required exp for that next level by 10
"src.Level+=1" raises their actual level
"src<<"You are now Level [src.Level]"" lets them know what level they just reached.
* The source (src) is most likely a Player here, but you could use this same proc to level enemies or other NPCs if you wanted.

Now we need a way to see this working.
Since we don't have an actual battle system yet, lets just make a quick test verb to add some experience to our character.
Step 2.3 Add the following code:
mob/proc
LevelCheck()
if(src.Exp>=src.Nexp)
src.Exp=0
src.Nexp+=10
src.Level+=1
src<<"You are now Level [src.Level]"

//new code below
mob/verb
Test_Leveling()
usr.Exp+=100
usr.LevelCheck()

This is just a simple test verb, so you can remove it whenever you want.
What it does is give the user 100 Exp and then run the LevelCheck() proc we just wrote on them.


Implementing New Additions
Finally, we'll implement this new Level variable into the Who verb.

Step 3.1 Move to "Verbs.dm" from the file tree

Step 3.2 Modify the following code, which can be found on line 12 (press Alt+G to move to a specific line)
    Who()
var/counter=0
for(var/mob/Player/M in world)
counter+=1
usr<<"([M.Level]) [M]" //this line has been modified
usr<<"<b>[counter] Players Online"

The newly added "([M.Level]) " will display the players' levels before their names. ie: (99) Falacy


Continue to Tutorial #4: Displaying Stats
Ehh, Falacy, you mind if I can post here on how to make a game? Lol. I will use the // note inside the DM Codes. >.> That way people know what the codes do :o
In response to Kingmasherr
yea i u can i want to know how to make one
Who verb doesnt work :c
It should work just fine as long as types of /mob/Player exist in the world. Although a shorter version would be

mob
verb
Who()
var/counter = 0
for(var/mob/Player/M) // Defaults to "in world"
counter ++
usr << "([M.Level]) [M]"
usr << "[counter] Players Online!
/* usr is acceptable in this case because
A. It's not a proc and
B. The usr is the person you want to see this */
You know how it says this?:

if(src.Exp>=src.Nexp)
src.Exp=0

Well, wouldn't it be better if it was this?:

if(src.Exp>=src.Nexp)
src.Exp=src.Exp-=src.Nexp

Because, a lot of times, people would want to keep the excess experience... I am just suggesting such a thing...
Even better:

while(src.experience>=src.next_level)
src.LevelUp()


mob/proc/LevelUp()
src.level++
src.experience -= src.next_level
src.next_level *= 1.1
is it better to have this as a while loop constantly checking or for us to call the levelup procedure every time the player gains exp. I think this would be more ideal:

mob/proc/Gainexp(var ammount)   
src.experience += ammount
if(src.experience>=src.next_level)
src.LevelUp()


mob/proc/LevelUp()
src.level++
src.experience -= src.next_level
src.next_level *= 1.1
Mine isn't a while loop that constantly checks for possible levelup. It's a loop that will alow you to level up more than once if your experience exceeds the next level amount.

mob/proc/Gainexp(var ammount)   
src.experience += ammount
while(src.experience>=src.next_level)
src.LevelUp()


mob/proc/LevelUp()
src.level++
src.experience -= src.next_level
src.next_level *= 1.1


It doesn't run constantly. It only runs until your experience is less than next_level. That way, if you add 10 million experience to the player's experience all at once, the player will level up to the level that would have put them at, rather than just doing it once.
In response to Ter13
Ah I realized when I looked at your post again, I think I saw while and assumed it was going to be a while(true) or something similarly infinite. How does the code reiterate back into the while loop after it breaks? surely you would need another loop ontop of that one?

After src.exp > nextlevel doesnt the code enter the levelup() proc and then terminate the while loop afterwards?, I feel like I am missing something obvious here

The while loop will iterate as long as the conditions are true. Meaning, it will work like an if() statement provided the user can't level up more than once in a single pass. If the user can level up more than once in a single pass, it will keep going until the user's experience is < next_level. Calling LevelUp() in no way breaks the loop. It only potentially changes the conditions upon which the loop depends.