Sign in to follow this  
Sh4d0wS14d3

Leveling System Needs Better

Recommended Posts

Sh4d0wS14d3    2

I've been working on my Snivy here and there, and I figure that the base system I used for the leveling needs some...adjusting.

And it's a little because gaining any number of experience triggers the level-up sound, and that's pretty much entirely my fault.

local function levelexp(inst)
	if inst.exp == 0 then
		startinglevel(inst)
	end
if inst:HasTag("pokemon") and inst:HasTag("mediumslowexp") then
        if inst.exp <8 then
        inst:AddTag("level1")
	    inst.level = 1

        elseif inst.exp >= 8 and inst.exp < 27 then
        inst:AddTag("level2")
        inst:RemoveTag("level1")
	    inst.level = 2

        inst.SoundEmitter:PlaySound("snivy/characters/snivy/levelup")

        elseif inst.exp >= 27 and inst.exp < 64 then
		inst:AddTag("level3")
        inst:RemoveTag("level2")
	    inst.level = 3
                                               
-- Skipping levels 4-97

		elseif inst.exp >= 970299 and inst.exp < 1000000 then
        inst:AddTag("level99")
        inst:RemoveTag("level98")
	    inst.level = 99
        inst.SoundEmitter:PlaySound("snivy/characters/snivy/levelup")
		
		elseif inst.exp >=1000000 then
        inst:AddTag("level100")
        inst:RemoveTag("level99")
	    inst.level = 100
	    inst.exp = 1000000
        inst.SoundEmitter:PlaySound("snivy/characters/snivy/levelup")
end
end      

Could I get some help figuring out a more reasonable method of setting level ups?

Share this post


Link to post
Share on other sites
CarlZalph    3,346

Instead of using tags, I'd advise you use a networked variable of 8bits.  This would let you have 256 unique levels and the server only networks a byte for the data instead of a whole string.

Also your code can remove half of your compares since the else statement goes if the condition is not true: if (N < 10) is false, then (N >= 10) is true always and doesn't need to be checked.

 

As for how to handle leveling up, I'd store the minimum xp values for the next level and use those as a reference point instead of a bit if-then-else nest.

Example LUA:

local xp = 0
local lvl = 0


local minxpforlvl = {
  1, 5, 10, 50
}
local function deltalvl(oldlvl, newlvl)
  if newlvl > oldlvl then
    print(string.format("Level up! [%i]->[%i]", oldlvl, newlvl))
  elseif oldlvl > newlvl then
    print(string.format("Level down! [%i]->[%i]", oldlvl, newlvl))
  else
    print(string.format("Level unchanged! [%i]->[%i]", oldlvl, newlvl))
  end
end


local function addxp(newxp)
  print(string.format("Adding xp: %i [%i]", newxp, xp + newxp))
  xp = xp + newxp
  while true
  do
    local xpfornextlvl = minxpforlvl[lvl + 1]
    if xpfornextlvl == nil then return end
    if xp >= xpfornextlvl
    then
      local oldlvl = lvl
      lvl = lvl + 1
      deltalvl(oldlvl, lvl)
    else
      break
    end
  end
end

for i=0,50
do
  addxp(1)
end

Prints:

Spoiler

Adding xp: 1 [1]
Level up! [0]->[1]
Adding xp: 1 [2]
Adding xp: 1 [3]
Adding xp: 1 [4]
Adding xp: 1 [5]
Level up! [1]->[2]
Adding xp: 1 [6]
Adding xp: 1 [7]
Adding xp: 1 [8]
Adding xp: 1 [9]
Adding xp: 1 [10]
Level up! [2]->[3]
Adding xp: 1 [11]
Adding xp: 1 [12]
Adding xp: 1 [13]
Adding xp: 1 [14]
Adding xp: 1 [15]
Adding xp: 1 [16]
Adding xp: 1 [17]
Adding xp: 1 [18]
Adding xp: 1 [19]
Adding xp: 1 [20]
Adding xp: 1 [21]
Adding xp: 1 [22]
Adding xp: 1 [23]
Adding xp: 1 [24]
Adding xp: 1 [25]
Adding xp: 1 [26]
Adding xp: 1 [27]
Adding xp: 1 [28]
Adding xp: 1 [29]
Adding xp: 1 [30]
Adding xp: 1 [31]
Adding xp: 1 [32]
Adding xp: 1 [33]
Adding xp: 1 [34]
Adding xp: 1 [35]
Adding xp: 1 [36]
Adding xp: 1 [37]
Adding xp: 1 [38]
Adding xp: 1 [39]
Adding xp: 1 [40]
Adding xp: 1 [41]
Adding xp: 1 [42]
Adding xp: 1 [43]
Adding xp: 1 [44]
Adding xp: 1 [45]
Adding xp: 1 [46]
Adding xp: 1 [47]
Adding xp: 1 [48]
Adding xp: 1 [49]
Adding xp: 1 [50]
Level up! [3]->[4]
Adding xp: 1 [51]

 

for i=0,50
do
  addxp(100)
end

Prints:

Spoiler

Adding xp: 100 [100]
Level up! [0]->[1]
Level up! [1]->[2]
Level up! [2]->[3]
Level up! [3]->[4]
Adding xp: 100 [200]
Adding xp: 100 [300]
Adding xp: 100 [400]
<more>

 

Note that this code for the function 'addxp' makes the assumption that you can't lose experience or downgrade a level.

That's left up to the reader.

Edited by CarlZalph
  • Thanks 1

Share this post


Link to post
Share on other sites
Sh4d0wS14d3    2

I don't actually remember making this thread, probably sleep-deprived or something by the looks of the title, but I certainly needed the help. Sorry for not responding for so long, and thanks.

A couple of problems I had though are trying to get the level-up sound to work, alongside the system actually leveling up.

unknown.png

And a less pertinent question since I'm a fool and already put in the numbers, can I put in a formula for the minxpforlevel, or would that just break?

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this