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