A simple tutorial: XP and levels

HakujinHakujin Join Date: 2003-05-09 Member: 16157Members, Constellation
edited April 2010 in Modding
The NS2 Engine Test is lacking what every single video game needs: experience points. So let's add some in this mini-mod. We need to know several things:

1) how do we track XP
2) how do we add XP
3) how do we display XP
4) what effect does XP have on the player?

I have decided to track XP on the player, award it when they kill a target dummy, display it on the HUD, and have levels gained boost the player's movement speed. Let start with tracking XP in Player.lua:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// add to Player.networkVars

level        = "integer"
xp        = "integer"


// set the initial values
// add to function Player:OnInit()

self.level    = 1
self.xp    = 0<!--c2--></div><!--ec2-->

So now we are tracking player XP and levels. Now we need a way to change the values. Despite advances in player reward systems seen in games like BF2:BC2, we will only reward players for doing what they do best: killing stuff. This also lets us use a single function to track when stuff is killed, and award XP to the killer. This function doesn't seem to fit any specific player or victim, so we will make it part of the Game class, in Game.lua:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function Game:AwardKillXP(attacker, victim)
    attacker.xp = attacker.xp + victim.XPvalue  // retrieve the XP class variable and add it to attacker's XP
    attacker:CheckLevelUp()  // whenever we award XP, check for a level up
end<!--c2--></div><!--ec2-->

But this function isn't ready for primetime, yet. We need to add the class variable 'XPvalue' to our victims - in this case, the target dummies. We will also need to call this function and pass it the attacker and victim. A great place to do this is where the player shoots the target. In Target.lua:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// add XP value
Target.XPvalue = 10

// if Server (not the client)
// add to function Target:TakeDamage(attacker, damage, doer, point, direction)
Game.instance:AwardKillXP(attacker, self)<!--c2--></div><!--ec2-->

So now when the Target is killed (they die when taking any damage), the Game class will be given the player and victim, add the victim's XP value, and have the attacker check for level-ups. Different objects may level up differently, so I added that function in Player.lua (plus what to do when leveling up):

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function Player:CheckLevelUp()
    if self.xp == 50 or self.xp == 100 or self.xp == 150 then  //a very primitive way to see if xp = level requirements
        self.level = self.level + 1   // add a level
        self:LevelUp()   // make something happen when we level up
    end
end

function Player:LevelUp()
    self.moveSpeed            =  2 * self.moveSpeed            //these dont have much effect, actually
    self.moveAcceleration     =  2 * self.moveAcceleration
    self.jumpHeight           =  self.jumpHeight + 1
        self.gravity               = self.gravity / 2
end<!--c2--></div><!--ec2-->

So how can we display all of this fancy information? We will re-purpose the player HUD to avoid the hassle of any Flash programming. We know the default HUD displays the game time and score in the top left corner. We are just going to change the score line to show the info we want, in PlayerUI.lua:

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->function PlayerUI_GetScore()

    local player = Client.GetLocalPlayer()
    //return player.score  //commented out the old value
    return "Level: "..player.level.."  ("..player.xp.." XP) - CLIPS: "..player.numRifleClips   //added a bunch of values
end<!--c2--></div><!--ec2-->

I have concatenated strings in "" with values using '..' to create a whole status line. Not fancy, but it does the job here. (note that the CLIPS - section assumes you have integrated the simple tutorial on clips; omit this part if you have not)

Congratulations! You now have a working XP / Level system that gives player bonuses and reports all information on the HUD. You could have the level ups into anything you wanted (except animation speed, AFAIK), or even create a menu system to let the player choose how to improve his character.

Comments

  • PhasePhase Join Date: 2003-11-18 Member: 23149Members, Constellation
    Nice tutorial, now I want to see one on how to create a mini-menu type thing for the player ;)
  • SlycasterSlycaster Limited Edition Join Date: 2002-01-24 Member: 24Members, NS1 Playtester
    Hakujin you are fantastic, can't wait to see what you come up with post-release
  • ZekZek Join Date: 2002-11-10 Member: 7962Members, NS1 Playtester, Constellation, Reinforced - Shadow
    I think recreating Combat is the best idea for a mod once the engine gets to a playable state. For all people complained about it in NS, I don't think anybody wants to mod the RTS stuff and Combat worked even just as Skulks vs. Marines.
Sign In or Register to comment.