Help making code efficient and simplifying XP and levelling
5 replies, posted
Hi. Recently i have been trying to implement XP and levelling into the gamemode i am creating. I am able to assign the player their level as well as give them XP for doing damage to enemies which is currently damage done divided by two.
Basically the code works well however it is not very efficient meaning i will have to write hundreds of lines of code for each level doing it this way.
Does anyone have and ideas on how i can simplify this process and make the code more efficient?
Here is the xp values for each level
[CODE]
lvl1 = 0
lvl2 = 83
lvl3 = 174
lvl4 = 276
lvl5 = 388
lvl6 = 512
lvl7 = 650
lvl8 = 801
lvl9 = 969
lvl10 = 1154
lvl11 = 1358
lvl12 = 1584
lvl13 = 1833
lvl14 = 2107
lvl15 = 2411
lvl16 = 2746
lvl17 = 3115
lvl18 = 3523
lvl19 = 3973
lvl20 = 4470
lvl21 = 5018
lvl22 = 5624
lvl23 = 6291
lvl24 = 7028
lvl25 = 7842
lvl26 = 8740
lvl27 = 9730
lvl28 = 10824
lvl29 = 12031
lvl30 = 13363
[/CODE]
This code is then using these values and checking them against their XP and tells me if they are above the level.
If i was to do this for every level as well as assign the player the actual level too. It would require a lot of lines of code. Is there a simpler way to do this?
[CODE]
function meta:addgunxp(amount)
local currentxp = self:getgunxp()
self:setgunxp( currentxp + amount )
end
function meta:setgunxp(amount)
self:SetNetworkedInt( "gunxp", amount )
self:savexp()
end
function meta:getgunxp()
return self:GetNetworkedInt("gunxp")
end
function meta:checkxp()
local currentxp = self:getgunxp()
if currentxp > lvl1 then self:ChatPrint("above level 1") elseif currentxp > lvl20 then self:ChatPrint("above level 20") end
end
function meta:savexp()
local xp = self:getgunxp()
self:SetPData("gunxp", xp)
end
[/CODE]
Try something like this
[LUA]
local levels = {
[1] = 0,
[2] = 83,
[3] = 174,
[4] = 276,
[5] = 388,
[6] = 512,
[7] = 650,
[8] = 801,
[9] = 969,
[10] = 1154,
[11] = 1358,
[12] = 1584,
[13] = 1833,
[14] = 2107,
[15] = 2411,
[16] = 2746,
[17] = 3115,
[18] = 3523,
[19] = 3973,
[20] = 4470,
[21] = 5018,
[22] = 5624,
[23] = 6291,
[24] = 7028,
[25] = 7842,
[26] = 8740,
[27] = 9730,
[28] = 10824,
[29] = 12031,
[30] = 13363
}
function meta:checkxp()
local currentxp = self:getgunxp()
local lastLevel = 0
for k,v in pairs(levels) do
if currentxp > v then
lastLevel = k
else
break
end
end
self:ChatPrint("above level "..lastLevel)
end
[/LUA]
This looks promising. I'll give it a go, thank you very much.
[editline]5th August 2016[/editline]
[QUOTE=rtm516;50835449]Try something like this
[LUA]
local levels = {
[1] = 0,
[2] = 83,
[3] = 174,
[4] = 276,
[5] = 388,
[6] = 512,
[7] = 650,
[8] = 801,
[9] = 969,
[10] = 1154,
[11] = 1358,
[12] = 1584,
[13] = 1833,
[14] = 2107,
[15] = 2411,
[16] = 2746,
[17] = 3115,
[18] = 3523,
[19] = 3973,
[20] = 4470,
[21] = 5018,
[22] = 5624,
[23] = 6291,
[24] = 7028,
[25] = 7842,
[26] = 8740,
[27] = 9730,
[28] = 10824,
[29] = 12031,
[30] = 13363
}
function meta:checkxp()
local currentxp = self:getgunxp()
local lastLevel = 0
for k,v in pairs(levels) do
if currentxp > v then
lastLevel = k
else
break
end
end
self:ChatPrint("above level "..lastLevel)
end
[/LUA][/QUOTE]
This looks promising. I'll give it a go, thank you very much.
[editline]5th August 2016[/editline]
[QUOTE=rtm516;50835449]Try something like this
[LUA]
local levels = {
[1] = 0,
[2] = 83,
[3] = 174,
[4] = 276,
[5] = 388,
[6] = 512,
[7] = 650,
[8] = 801,
[9] = 969,
[10] = 1154,
[11] = 1358,
[12] = 1584,
[13] = 1833,
[14] = 2107,
[15] = 2411,
[16] = 2746,
[17] = 3115,
[18] = 3523,
[19] = 3973,
[20] = 4470,
[21] = 5018,
[22] = 5624,
[23] = 6291,
[24] = 7028,
[25] = 7842,
[26] = 8740,
[27] = 9730,
[28] = 10824,
[29] = 12031,
[30] = 13363
}
function meta:checkxp()
local currentxp = self:getgunxp()
local lastLevel = 0
for k,v in pairs(levels) do
if currentxp > v then
lastLevel = k
else
break
end
end
self:ChatPrint("above level "..lastLevel)
end
[/LUA][/QUOTE]
Works perfectly Thanks!
[IMG]https://i.gyazo.com/b2b460be0db118a76885141af6212fc5.png[/IMG]
[QUOTE=lewiso574;50835471]This looks promising. I'll give it a go, thank you very much.
[editline]5th August 2016[/editline]
This looks promising. I'll give it a go, thank you very much.
[editline]5th August 2016[/editline]
Works perfectly Thanks!
[IMG]https://i.gyazo.com/b2b460be0db118a76885141af6212fc5.png[/IMG][/QUOTE]
Welcome, glad i could help.
[editline]5th August 2016[/editline]
Also advice you should use Get/SetNWInt instead of Get/SetNetworkedInt because Get/SetNetworkedInt is deprecated
You're better off using a formula to calculate the experience.
I found the best model for your leveling up experience to be:
f(x)=(x*3.77)^2
You can see Desmos thinks so too:
[t]http://puu.sh/qrhtb.png[/t]
Your next step would be to create the function as a Lua function, something along the lines of...
[code]
function getLevelExperience( level )
return ( level * 3.77 ) ^ 2
end
function getLevelBounds( level )
-- min, max
return getLevelExperience( level ), getLevelExperience( level + 1 )
end
[/code]
Sorry, you need to Log In to post a reply to this thread.