This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CCJJSax's profile picture

Player Leveling System

Started by CCJJSax, 08 August 2013 - 09:42 PM
CCJJSax #1
Posted 08 August 2013 - 11:42 PM
I'm trying to make a game with a player experience/level system. I'm struggling to find the best way to do it. With what I have below, level 1 works, but when you get to an experience level above 10, it's still true under the if. I tried doing it with an and, but that kept erroring, and I don't know why.

There has to be a better way to do it. also Is there an easier way to do this? I thought perhaps you could make a table for it, but my friend said tables are system resource heavy.


function playerLevel()
if player_Experience >= 0 then
  if player_Experience < 10 then
   player_level = 1
  end
elseif player_Experience >= 10 then
  if player_Experience < 20 then
   player_level = 2
  end
--[[ elseif player_Experience >= 20 and < 40 then
  player_level = 3
elseif player_Experience >= 40 and < 70 then
  player_level = 4
elseif player_Experience >= 70 and < 120 then
  player_level = 5
elseif player_Experience >= 120 and < 180 then
  player_level = 6
elseif player_Experience >= 180 and < 240 then
  player_level = 7
elseif player_Experience >= 240 and < 310 then
  player_level = 8
elseif player_Experience >= 310 and < 390 then
  player_level = 9
elseif player_Experience >= 390 and < 400 then
  player_level = 10
elseif player_Experience >= 400 and < 510 then
  player_level = 11
elseif player_Experience >= 510 and < 640 then
  player_level = 12]]
end
end
colt_419 #2
Posted 09 August 2013 - 12:01 AM
I am not an expert or pro, but, maybe removing the ands might work.

function playerLevel()
if player_Experience >= 0 then
  if player_Experience < 10 then
   player_level = 1
  end
elseif player_Experience >= 10 then
  if player_Experience < 20 then
   player_level = 2
  end
--[[ elseif player_Experience >= 20  then
  player_level = 3
elseif player_Experience >= 40 then
  player_level = 4
elseif player_Experience >= 70 then
  player_level = 5
elseif player_Experience >= 120 then
  player_level = 6
elseif player_Experience >= 180 then
  player_level = 7
elseif player_Experience >= 240 then
  player_level = 8
elseif player_Experience >= 310 then
  player_level = 9
elseif player_Experience >= 390 then
  player_level = 10
elseif player_Experience >= 400 then
  player_level = 11
elseif player_Experience >= 510
then
  player_level = 12]]
end
end

or Maybe not putting ands but a ',' instead

function playerLevel() 
if player_Experience >= 0 then 
  if player_Experience < 10 then 
   player_level = 1 
  end 
elseif player_Experience >= 10 then 
  if player_Experience < 20 then 
   player_level = 2 
  end 
--[[ elseif player_Experience >= 20,< 40 then 
  player_level = 3 
elseif player_Experience >= 40,< 70 then 
  player_level = 4 
elseif player_Experience >= 70,< 120 then 
  player_level = 5 
elseif player_Experience >= 120,< 180 then 
  player_level = 6 
elseif player_Experience >= 180,< 240 then 
  player_level = 7 
elseif player_Experience >= 240,< 310 then 
  player_level = 8 
elseif player_Experience >= 310,< 390 then 
  player_level = 9 
elseif player_Experience >= 390,< 400 then 
  player_level = 10 
elseif player_Experience >= 400,< 510 then 
  player_level = 11 
elseif player_Experience >= 510,< 640 then 
  player_level = 12]] 
end 
end


I dont know if this will work at all, just trying to help find an answer.
CCJJSax #3
Posted 09 August 2013 - 12:07 AM
I am not an expert or pro, but, maybe removing the ands might work.

function playerLevel()
if player_Experience >= 0 then
  if player_Experience < 10 then
   player_level = 1
  end
elseif player_Experience >= 10 then
  if player_Experience < 20 then
   player_level = 2
  end
--[[ elseif player_Experience >= 20  then
  player_level = 3
elseif player_Experience >= 40 then
  player_level = 4
elseif player_Experience >= 70 then
  player_level = 5
elseif player_Experience >= 120 then
  player_level = 6
elseif player_Experience >= 180 then
  player_level = 7
elseif player_Experience >= 240 then
  player_level = 8
elseif player_Experience >= 310 then
  player_level = 9
elseif player_Experience >= 390 then
  player_level = 10
elseif player_Experience >= 400 then
  player_level = 11
elseif player_Experience >= 510
then
  player_level = 12]]
end
end

or Maybe not putting ands but a ',' instead

[color="#000088"]function[/color][color="#000000"] playerLevel[/color][color="#666600"]()[/color] [color="#000088"]if[/color][color="#000000"] player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]0[/color] [color="#000088"]then[/color] [color="#000088"]if[/color][color="#000000"] player_Experience [/color][color="#666600"]<[/color] [color="#006666"]10[/color] [color="#000088"]then[/color][color="#000000"]
   player_level [/color][color="#666600"]=[/color] [color="#006666"]1[/color] [color="#000088"]end[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]10[/color] [color="#000088"]then[/color] [color="#000088"]if[/color][color="#000000"] player_Experience [/color][color="#666600"]<[/color] [color="#006666"]20[/color] [color="#000088"]then[/color][color="#000000"]
   player_level [/color][color="#666600"]=[/color] [color="#006666"]2[/color] [color="#000088"]end[/color] [color="#666600"]--[[[/color][color="#000000"] elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]20[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]40[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]3[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]40[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]70[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]4[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]70[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]120[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]5[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]120[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]180[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]6[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]180[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]240[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]7[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]240[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]310[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]8[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]310[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]390[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]9[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]390[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]400[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]10[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]400[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]510[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]11[/color][color="#000000"]
elseif player_Experience [/color][color="#666600"]>=[/color] [color="#006666"]510[/color][color="#000000"],[/color] [color="#666600"]<[/color] [color="#006666"]640[/color] [color="#000088"]then[/color][color="#000000"]
  player_level [/color][color="#666600"]=[/color] [color="#006666"]12[/color][color="#666600"]]][/color] [color="#000088"]end[/color] [color="#000088"]end
[/color]
I dont know if this will work at all, just trying to help find an answer.

Those color codes and everything makes that hard to read XD. I had the same issue with my original post too, so I just undid the colors.

I thought of doing that same thing, as in your first post, but I'll show you why I found that won't work.


function playerLevel()
if player_Experience >= 0 then -- it always checks this first, and player_Experience 800 is above this, so it will never hit the next elseif.
  if player_Experience < 10 then
   player_level = 1
  end
elseif player_Experience >= 10 then
  if player_Experience < 20 then
   player_level = 2
  end
end
end
colt_419 #4
Posted 09 August 2013 - 12:35 AM
I see.. Hmm, maybe take away the elseifs altogether and just put ifs..
If you put elseif it wont go on after the first if works.. But with just if it will go down the list every time.
Or put your Xp level back wards.. Like instead of >=0 put >=1000 and go down, that way the 1st one wont always be true.
CCJJSax #5
Posted 09 August 2013 - 01:32 AM
I see.. Hmm, maybe take away the elseifs altogether and just put ifs..
If you put elseif it wont go on after the first if works.. But with just if it will go down the list every time.
Or put your Xp level back wards.. Like instead of >=0 put >=1000 and go down, that way the 1st one wont always be true.

That was my idea I was just about to post, then some code my friend gave me awhile back finally clicked. His way is Sooooo much better than this. I just needed to learn what "return" actually did. It's self explanatory, but I just didn't get it for some reason lol. so I've been testing it. here it is.


player_level = 1

function sleep(seconds)
local _start = os.time()
local _end = _start+seconds
while (_end ~= os.time()) do
end
end

local function getExpToLvlUp(level)  -- to be added and implimented
if player_level %10 == 0 then
print("10")
return player_level*player_level - 2*player_level+20
else
return player_level*player_level - 2*player_level+10
end
end

for i = 1, 80 do
print("Player level: "..player_level.." \- xp needed "..getExpToLvlUp())
player_level = player_level + 1
sleep(1)
end

MR_nesquick #6
Posted 09 August 2013 - 07:49 AM

if player_Experience >= 0 and player_Experience < 10 then
   player_level = 1
elseif
  player_Experience >= 10 and player_Experience < 20 then
  player_level = 2

this would check if the input is between the two values
not sure why you didn't use it on level 1 and 2 though.
Vilsol #7
Posted 09 August 2013 - 08:33 AM
One way to do it is this

if exp < 10 then
level = 1
elseif exp < 20 then
level = 2
elseif exp < 30 then
level = 3
etc...

But what I think is a better way to do is

levels = {}
levels[1] = 10
levels[2] = 20
levels[3] = 30
levels[4] = 40
levels[5] = 50
levels[6] = 60
...
function getLevel(xp)
for level, exp in ipairs(levels) do
if xp <=  exp then
return level
end
end
end
TheOddByte #8
Posted 09 August 2013 - 09:52 AM
May I suggest that you do something like this?

-- Starting values
local xp = 0
local tXp = 10
local level = 1
local maxLevel = 18 -- Like in LoL :P/>

--[[
Checking if the xp is equal or more than the total xp
And if it is the level increase by 1, the total xp by 10 
And the xp returns to 0.
]]--
if xp >= tXp then xp = 0; tXp = tXp + 10; level = level + 1 
 if level > maxLevel then level = maxLevel end
end
I hope you understand that small piece of code..
It basically sets some default values first and below that it checkd if you are going to level up..
This is much more effiecient than the way you checked if you were going to level up since here you can simply set a max level above and don't have todo an 'if' statement for every level ;)/>
Also if you don't want the xp to go back to 0 every time then just remove that 'xp = 0;' in the if statement
Also.. ';' isn't required in Lua, It's just a habit from another coding language so you know and don't get confused..
Vilsol #9
Posted 09 August 2013 - 11:30 AM
May I suggest that you do something like this?

-- Starting values
local xp = 0
local tXp = 10
local level = 1
local maxLevel = 18 -- Like in LoL :P/>/>

--[[
Checking if the xp is equal or more than the total xp
And if it is the level increase by 1, the total xp by 10
And the xp returns to 0.
]]--
if xp >= tXp then xp = 0; tXp = tXp + 10; level = level + 1
if level > maxLevel then level = maxLevel end
end
I hope you understand that small piece of code..
It basically sets some default values first and below that it checkd if you are going to level up..
This is much more effiecient than the way you checked if you were going to level up since here you can simply set a max level above and don't have todo an 'if' statement for every level ;)/>
Also if you don't want the xp to go back to 0 every time then just remove that 'xp = 0;' in the if statement
Also.. ';' isn't required in Lua, It's just a habit from another coding language so you know and don't get confused..

With this setup you might have some problems for example.
People can still collect XP when they are at level 18
And what if I want to make it so to reach level 2 you need 10 xp but level 3 100 xp?
TheOddByte #10
Posted 09 August 2013 - 12:12 PM
May I suggest that you do something like this?

-- Starting values
local xp = 0
local tXp = 10
local level = 1
local maxLevel = 18 -- Like in LoL :P/>/>/>

--[[
Checking if the xp is equal or more than the total xp
And if it is the level increase by 1, the total xp by 10
And the xp returns to 0.
]]--
if xp >= tXp then xp = 0; tXp = tXp + 10; level = level + 1
if level > maxLevel then level = maxLevel end
end
I hope you understand that small piece of code..
It basically sets some default values first and below that it checkd if you are going to level up..
This is much more effiecient than the way you checked if you were going to level up since here you can simply set a max level above and don't have todo an 'if' statement for every level ;)/>/>
Also if you don't want the xp to go back to 0 every time then just remove that 'xp = 0;' in the if statement
Also.. ';' isn't required in Lua, It's just a habit from another coding language so you know and don't get confused..

With this setup you might have some problems for example.
People can still collect XP when they are at level 18
And what if I want to make it so to reach level 2 you need 10 xp but level 3 100 xp?
It was only a suggestion, And I know you could still gain xp at the max level but that's just depending on how he makes it when the player gains xp..
He could have like level checkpoints so the total xp would add by more at a certain level..
I just think this is simpler for him if he want it to be lots of levels instead of having to code every level from 1-50 or something… But hey, It's his decision so..
CCJJSax #11
Posted 10 August 2013 - 02:32 AM

if player_Experience >= 0 and player_Experience < 10 then
   player_level = 1
elseif
  player_Experience >= 10 and player_Experience < 20 then
  player_level = 2

this would check if the input is between the two values
not sure why you didn't use it on level 1 and 2 though.

I don't know why, but I can't seem to get any "and"'s or "or"'s working in real situations like this. I can do something like this


a = 1
b = 3
c = 5
if a < b and c > b then
  print("true")
else
  print("false")
end


But when I put that same type of thing into a needed place like here, it doesn't work. Don't ask why. I tried exactly what you said, and it failed. It baffles me too.
CCJJSax #12
Posted 10 August 2013 - 02:34 AM
One way to do it is this

if exp < 10 then
level = 1
elseif exp < 20 then
level = 2
elseif exp < 30 then
level = 3
etc...

But what I think is a better way to do is

levels = {}
levels[1] = 10
levels[2] = 20
levels[3] = 30
levels[4] = 40
levels[5] = 50
levels[6] = 60
...
function getLevel(xp)
for level, exp in ipairs(levels) do
if xp <=  exp then
return level
end
end
end

I think you mean inpairs(levels) ;)/> I was going to do that, but my friend said that tables are system resource heavy, so I've been trying to avoid them. Can you confirm this?
CCJJSax #13
Posted 10 August 2013 - 02:39 AM
May I suggest that you do something like this?

-- Starting values
local xp = 0
local tXp = 10
local level = 1
local maxLevel = 18 -- Like in LoL :P/>/>

--[[
Checking if the xp is equal or more than the total xp
And if it is the level increase by 1, the total xp by 10
And the xp returns to 0.
]]--
if xp >= tXp then xp = 0; tXp = tXp + 10; level = level + 1
if level > maxLevel then level = maxLevel end
end
I hope you understand that small piece of code..
It basically sets some default values first and below that it checkd if you are going to level up..
This is much more effiecient than the way you checked if you were going to level up since here you can simply set a max level above and don't have todo an 'if' statement for every level ;)/>
Also if you don't want the xp to go back to 0 every time then just remove that 'xp = 0;' in the if statement
Also.. ';' isn't required in Lua, It's just a habit from another coding language so you know and don't get confused..

I noticed the ";" and the "/>" from Html, I think (probably wrong, I only know Lua to a decent level)
That's a good suggestion. I'll consider this one too.
TheOddByte #14
Posted 10 August 2013 - 05:07 AM
May I suggest that you do something like this?

-- Starting values
local xp = 0
local tXp = 10
local level = 1
local maxLevel = 18 -- Like in LoL :P/>/>/>/>

--[[
Checking if the xp is equal or more than the total xp
And if it is the level increase by 1, the total xp by 10
And the xp returns to 0.
]]--
if xp >= tXp then xp = 0; tXp = tXp + 10; level = level + 1
if level > maxLevel then level = maxLevel end
end
I hope you understand that small piece of code..
It basically sets some default values first and below that it checkd if you are going to level up..
This is much more effiecient than the way you checked if you were going to level up since here you can simply set a max level above and don't have todo an 'if' statement for every level ;)/>/>/>
Also if you don't want the xp to go back to 0 every time then just remove that 'xp = 0;' in the if statement
Also.. ';' isn't required in Lua, It's just a habit from another coding language so you know and don't get confused..

I noticed the ";" and the "/>" from Html, I think (probably wrong, I only know Lua to a decent level)
That's a good suggestion. I'll consider this one too.
';' indicates end of the line kinda but though it's optional in Lua. '/>' is the html when adding a smiley in a code tag or when editing a post with a smiley