Post #1 · Posted at 2015-10-14 11:04:50pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
On my theme, I have implemented a kind of Level System, it is simple. You got a determinate level depending of the amount of DP you have saved on your profile. I would like to update this script with the following.
I still n00by when I have to use "for", so I had to made it a little manually, my system is limited to 100 levels. I have tried to make it to get 200, 500, or the max levels I want using "for" but, I always fail. The current script that determine the levels are the followings;
As you can see, i have to determinate each level manually using if EXP < EXP[5] then LV = 24 (as an example). I would like to remove that and using "for" make all automatically and like that way. Not hardcode the levels to 100.
If someone can help me, that would be nice.
I still n00by when I have to use "for", so I had to made it a little manually, my system is limited to 100 levels. I have tried to make it to get 200, 500, or the max levels I want using "for" but, I always fail. The current script that determine the levels are the followings;
function SystemLevelForLocalProfiles(p)
local Profile = PROFILEMAN:GetLocalProfileFromIndex(p)
local DancePoints = Profile:GetTotalDancePoints()
local XP = DancePoints
local LV
local current_level
local first_level = 1
for i=1,LevelSystemSettings["MaxLevel"] do
current_level = i
table.insert(LVLocal,current_level)
if XP < EXP[1] then LV = 0 elseif XP < EXP[2] then LV = 1 elseif XP < EXP[3] then LV = 2 elseif XP < EXP[4] then LV = 3 elseif XP < EXP[5] then LV = 4
elseif XP < EXP[6] then LV = 5 elseif XP < EXP[7] then LV = 6 elseif XP < EXP[8] then LV = 7 elseif XP < EXP[9] then LV = 8 elseif XP < EXP[10] then LV = 9
-- Levels 10 to 19.
elseif XP < EXP[11] then LV = 10 elseif XP < EXP[12] then LV = 11 elseif XP < EXP[13] then LV = 12 elseif XP < EXP[14] then LV = 13 elseif XP < EXP[15] then LV = 14
elseif XP < EXP[16] then LV = 15 elseif XP < EXP[17] then LV = 16 elseif XP < EXP[18] then LV = 17 elseif XP < EXP[19] then LV = 18 elseif XP < EXP[20] then LV = 19
-- Levels 20 to 29.
elseif XP < EXP[21] then LV = 20 elseif XP < EXP[22] then LV = 21 elseif XP < EXP[23] then LV = 22 elseif XP < EXP[24] then LV = 23 elseif XP < EXP[25] then LV = 24
elseif XP < EXP[26] then LV = 25 elseif XP < EXP[27] then LV = 26 elseif XP < EXP[28] then LV = 27 elseif XP < EXP[29] then LV = 28 elseif XP < EXP[30] then LV = 29
-- Levels 30 to 39.
elseif XP < EXP[31] then LV = 30 elseif XP < EXP[32] then LV = 31 elseif XP < EXP[33] then LV = 32 elseif XP < EXP[34] then LV = 33 elseif XP < EXP[35] then LV = 34
elseif XP < EXP[36] then LV = 35 elseif XP < EXP[37] then LV = 36 elseif XP < EXP[38] then LV = 37 elseif XP < EXP[39] then LV = 38 elseif XP < EXP[40] then LV = 39
-- Levels 40 to 49.
elseif XP < EXP[41] then LV = 40 elseif XP < EXP[42] then LV = 41 elseif XP < EXP[43] then LV = 42 elseif XP < EXP[44] then LV = 43 elseif XP < EXP[45] then LV = 44
elseif XP < EXP[46] then LV = 45 elseif XP < EXP[47] then LV = 46 elseif XP < EXP[48] then LV = 47 elseif XP < EXP[49] then LV = 48 elseif XP < EXP[50] then LV = 49
-- Levels 50 to 59.
elseif XP < EXP[51] then LV = 50 elseif XP < EXP[52] then LV = 51 elseif XP < EXP[53] then LV = 52 elseif XP < EXP[54] then LV = 53 elseif XP < EXP[55] then LV = 54
elseif XP < EXP[56] then LV = 55 elseif XP < EXP[57] then LV = 56 elseif XP < EXP[58] then LV = 57 elseif XP < EXP[59] then LV = 58 elseif XP < EXP[60] then LV = 59
-- Levels 60 to 69.
elseif XP < EXP[61] then LV = 60 elseif XP < EXP[62] then LV = 61 elseif XP < EXP[63] then LV = 62 elseif XP < EXP[64] then LV = 63 elseif XP < EXP[65] then LV = 64
elseif XP < EXP[66] then LV = 65 elseif XP < EXP[67] then LV = 66 elseif XP < EXP[68] then LV = 67 elseif XP < EXP[69] then LV = 68 elseif XP < EXP[70] then LV = 69
-- Levels 70 to 79.
elseif XP < EXP[71] then LV = 70 elseif XP < EXP[72] then LV = 71 elseif XP < EXP[73] then LV = 72 elseif XP < EXP[74] then LV = 73 elseif XP < EXP[75] then LV = 74
elseif XP < EXP[76] then LV = 75 elseif XP < EXP[77] then LV = 76 elseif XP < EXP[78] then LV = 77 elseif XP < EXP[79] then LV = 78 elseif XP < EXP[80] then LV = 79
-- Levels 80 to 89.
elseif XP < EXP[81] then LV = 80 elseif XP < EXP[82] then LV = 81 elseif XP < EXP[83] then LV = 82 elseif XP < EXP[84] then LV = 83 elseif XP < EXP[85] then LV = 84
elseif XP < EXP[86] then LV = 85 elseif XP < EXP[87] then LV = 86 elseif XP < EXP[88] then LV = 87 elseif XP < EXP[89] then LV = 88 elseif XP < EXP[90] then LV = 89
-- Levels 90 to 99.
elseif XP < EXP[91] then LV = 90 elseif XP < EXP[92] then LV = 91 elseif XP < EXP[93] then LV = 92 elseif XP < EXP[94] then LV = 93 elseif XP < EXP[95] then LV = 94
elseif XP < EXP[96] then LV = 95 elseif XP < EXP[97] then LV = 96 elseif XP < EXP[98] then LV = 97 elseif XP < EXP[99] then LV = 98 elseif XP < EXP[100] then LV = 99
-- Level MAX.
else LV = "MAX" end
end
return tostring(LV)
end
local Profile = PROFILEMAN:GetLocalProfileFromIndex(p)
local DancePoints = Profile:GetTotalDancePoints()
local XP = DancePoints
local LV
local current_level
local first_level = 1
for i=1,LevelSystemSettings["MaxLevel"] do
current_level = i
table.insert(LVLocal,current_level)
if XP < EXP[1] then LV = 0 elseif XP < EXP[2] then LV = 1 elseif XP < EXP[3] then LV = 2 elseif XP < EXP[4] then LV = 3 elseif XP < EXP[5] then LV = 4
elseif XP < EXP[6] then LV = 5 elseif XP < EXP[7] then LV = 6 elseif XP < EXP[8] then LV = 7 elseif XP < EXP[9] then LV = 8 elseif XP < EXP[10] then LV = 9
-- Levels 10 to 19.
elseif XP < EXP[11] then LV = 10 elseif XP < EXP[12] then LV = 11 elseif XP < EXP[13] then LV = 12 elseif XP < EXP[14] then LV = 13 elseif XP < EXP[15] then LV = 14
elseif XP < EXP[16] then LV = 15 elseif XP < EXP[17] then LV = 16 elseif XP < EXP[18] then LV = 17 elseif XP < EXP[19] then LV = 18 elseif XP < EXP[20] then LV = 19
-- Levels 20 to 29.
elseif XP < EXP[21] then LV = 20 elseif XP < EXP[22] then LV = 21 elseif XP < EXP[23] then LV = 22 elseif XP < EXP[24] then LV = 23 elseif XP < EXP[25] then LV = 24
elseif XP < EXP[26] then LV = 25 elseif XP < EXP[27] then LV = 26 elseif XP < EXP[28] then LV = 27 elseif XP < EXP[29] then LV = 28 elseif XP < EXP[30] then LV = 29
-- Levels 30 to 39.
elseif XP < EXP[31] then LV = 30 elseif XP < EXP[32] then LV = 31 elseif XP < EXP[33] then LV = 32 elseif XP < EXP[34] then LV = 33 elseif XP < EXP[35] then LV = 34
elseif XP < EXP[36] then LV = 35 elseif XP < EXP[37] then LV = 36 elseif XP < EXP[38] then LV = 37 elseif XP < EXP[39] then LV = 38 elseif XP < EXP[40] then LV = 39
-- Levels 40 to 49.
elseif XP < EXP[41] then LV = 40 elseif XP < EXP[42] then LV = 41 elseif XP < EXP[43] then LV = 42 elseif XP < EXP[44] then LV = 43 elseif XP < EXP[45] then LV = 44
elseif XP < EXP[46] then LV = 45 elseif XP < EXP[47] then LV = 46 elseif XP < EXP[48] then LV = 47 elseif XP < EXP[49] then LV = 48 elseif XP < EXP[50] then LV = 49
-- Levels 50 to 59.
elseif XP < EXP[51] then LV = 50 elseif XP < EXP[52] then LV = 51 elseif XP < EXP[53] then LV = 52 elseif XP < EXP[54] then LV = 53 elseif XP < EXP[55] then LV = 54
elseif XP < EXP[56] then LV = 55 elseif XP < EXP[57] then LV = 56 elseif XP < EXP[58] then LV = 57 elseif XP < EXP[59] then LV = 58 elseif XP < EXP[60] then LV = 59
-- Levels 60 to 69.
elseif XP < EXP[61] then LV = 60 elseif XP < EXP[62] then LV = 61 elseif XP < EXP[63] then LV = 62 elseif XP < EXP[64] then LV = 63 elseif XP < EXP[65] then LV = 64
elseif XP < EXP[66] then LV = 65 elseif XP < EXP[67] then LV = 66 elseif XP < EXP[68] then LV = 67 elseif XP < EXP[69] then LV = 68 elseif XP < EXP[70] then LV = 69
-- Levels 70 to 79.
elseif XP < EXP[71] then LV = 70 elseif XP < EXP[72] then LV = 71 elseif XP < EXP[73] then LV = 72 elseif XP < EXP[74] then LV = 73 elseif XP < EXP[75] then LV = 74
elseif XP < EXP[76] then LV = 75 elseif XP < EXP[77] then LV = 76 elseif XP < EXP[78] then LV = 77 elseif XP < EXP[79] then LV = 78 elseif XP < EXP[80] then LV = 79
-- Levels 80 to 89.
elseif XP < EXP[81] then LV = 80 elseif XP < EXP[82] then LV = 81 elseif XP < EXP[83] then LV = 82 elseif XP < EXP[84] then LV = 83 elseif XP < EXP[85] then LV = 84
elseif XP < EXP[86] then LV = 85 elseif XP < EXP[87] then LV = 86 elseif XP < EXP[88] then LV = 87 elseif XP < EXP[89] then LV = 88 elseif XP < EXP[90] then LV = 89
-- Levels 90 to 99.
elseif XP < EXP[91] then LV = 90 elseif XP < EXP[92] then LV = 91 elseif XP < EXP[93] then LV = 92 elseif XP < EXP[94] then LV = 93 elseif XP < EXP[95] then LV = 94
elseif XP < EXP[96] then LV = 95 elseif XP < EXP[97] then LV = 96 elseif XP < EXP[98] then LV = 97 elseif XP < EXP[99] then LV = 98 elseif XP < EXP[100] then LV = 99
-- Level MAX.
else LV = "MAX" end
end
return tostring(LV)
end
As you can see, i have to determinate each level manually using if EXP < EXP[5] then LV = 24 (as an example). I would like to remove that and using "for" make all automatically and like that way. Not hardcode the levels to 100.
If someone can help me, that would be nice.
Post #2 · Posted at 2015-10-14 11:18:42pm 9.9 years ago
Lua lacks from switch statements, that is the way that I could solve your situation, but searching in the web, there are some implementations that could be equivalent to the switch/case statement:
http://www.corsix.org/content/switch-lua-strings
http://lua-users.org/wiki/SwitchStatement
http://dev.oz-apps.com/?p=265
I think is more clear to code than a lot of nested if's.
http://www.corsix.org/content/switch-lua-strings
http://lua-users.org/wiki/SwitchStatement
http://dev.oz-apps.com/?p=265
I think is more clear to code than a lot of nested if's.
Post #3 · Posted at 2015-10-14 11:24:50pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
Hmm that still soo confused to me... I still have to learn how to use "for" so much. Also, since the english is not my main language, those links can be confused to me. Anyway, ill trying to how ti fix that to avoid the use of so many if's.
Post #4 · Posted at 2015-10-14 11:31:06pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
830 Posts | |
Not Set | |
Reg. 2009-11-24 | |
Ok, I'm a noob in Lua too, but reviewing you code, I think it could be something like this:
Quote
for i=1,LevelSystemSettings["MaxLevel"] do
if XP < i then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
if XP < i then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
Post #5 · Posted at 2015-10-14 11:34:09pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
nope... putting your code, it returns always 100.
Post #6 · Posted at 2015-10-14 11:42:39pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
830 Posts | |
Not Set | |
Reg. 2009-11-24 | |
Now?
Quote
for i=1,LevelSystemSettings["MaxLevel"] do
if XP < EXP[i] then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
if XP < EXP[i] then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
Post #7 · Posted at 2015-10-14 11:53:05pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
Quote: MadkaT
Now?
Quote
for i=1,LevelSystemSettings["MaxLevel"] do
if XP < EXP[i] then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
if XP < EXP[i] then
LV = i-1
else
if XP = LevelSystemSettings["MaxLevel"] then
LV = "MAX"
end
end
end
Nope, it returned always 99...
Omg, I figure it out, and it was so simple now i see it... Anyway, Thanks ou MadkaT for trying to help me.
for i=1,LevelSystemSettings["MaxLevel"] do
if XP > EXP[i] then
LV = i
end
end
if XP > EXP[i] then
LV = i
end
end
It returns the corresponding LV for each profile, i still needs to figure out how to solve the "nil" for newer profiles.
Post #8 · Posted at 2015-10-14 11:59:42pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
830 Posts | |
Not Set | |
Reg. 2009-11-24 | |
Well, at least I was near to the solution
for the nil value, just put an if befores this for to check the nil on XP, if xp is nil you can assign level 0 and in the else else of that if, add the for that you have created.

Post #9 · Posted at 2015-10-15 12:06:28am 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
Quote: MadkaT
Well, at least I was near to the solution
for the nil value, just put an if befores this for to check the nil on XP, if xp is nil you can assign level 0 and in the else else of that if, add the for that you have created.

Fixed, thanks you for your help!
Post #10 · Posted at 2015-10-15 12:36:24pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
413 Posts | |
![]() | |
Reg. 2008-01-13 | |
"I want to change my username =(" |
Last updated: 2015-10-15 12:37pm
Try using ">=" instead of just ">"
Oh, didn't read completely the last post...
Post #11 · Posted at 2015-10-16 04:29:03pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
65 Posts | |
![]() | |
Reg. 2011-10-26 | |
There are a few ways you could improve that, if you care at all about efficiency.
The first is to use the keyword break once you find the appropriate level, instead of checking all 100 levels every time. break exits the loop immediately.
The second thing would be to use a binary search rather than a linear one. That is, rather than counting through every level one by one, you can check the middle and see which directly you need to go from there, cutting the list in half each time. The following should take 6 checks to find the correct level anywhere on the list.
local min, max = 1, LevelSystemSettings.MaxLevel + 1;
while min < max do
local mid = math.floor( ( min + max ) / 2 );
if XP < EXP[mid] then
max = mid - 1;
else
min = mid + 1;
end
end
LV = min - 1;
The first is to use the keyword break once you find the appropriate level, instead of checking all 100 levels every time. break exits the loop immediately.
for i=1,LevelSystemSettings["MaxLevel"] do
if XP > EXP[i] then
LV = i
else
break;
end
end
if XP > EXP[i] then
LV = i
else
break;
end
end
The second thing would be to use a binary search rather than a linear one. That is, rather than counting through every level one by one, you can check the middle and see which directly you need to go from there, cutting the list in half each time. The following should take 6 checks to find the correct level anywhere on the list.
local min, max = 1, LevelSystemSettings.MaxLevel + 1;
while min < max do
local mid = math.floor( ( min + max ) / 2 );
if XP < EXP[mid] then
max = mid - 1;
else
min = mid + 1;
end
end
LV = min - 1;
Post #12 · Posted at 2015-10-16 05:28:03pm 9.9 years ago
![]() | |
---|---|
![]() |
Member |
313 Posts | |
![]() | |
Reg. 2012-09-13 | |
"ITG, The Best Dance Machine!" |
Quote: Mad Matt
There are a few ways you could improve that, if you care at all about efficiency.
The first is to use the keyword break once you find the appropriate level, instead of checking all 100 levels every time. break exits the loop immediately.
The second thing would be to use a binary search rather than a linear one. That is, rather than counting through every level one by one, you can check the middle and see which directly you need to go from there, cutting the list in half each time. The following should take 6 checks to find the correct level anywhere on the list.
local min, max = 1, LevelSystemSettings.MaxLevel + 1;
while min < max do
local mid = math.floor( ( min + max ) / 2 );
if XP < EXP[mid] then
max = mid - 1;
else
min = mid + 1;
end
end
LV = min - 1;
The first is to use the keyword break once you find the appropriate level, instead of checking all 100 levels every time. break exits the loop immediately.
for i=1,LevelSystemSettings["MaxLevel"] do
if XP > EXP[i] then
LV = i
else
break;
end
end
if XP > EXP[i] then
LV = i
else
break;
end
end
The second thing would be to use a binary search rather than a linear one. That is, rather than counting through every level one by one, you can check the middle and see which directly you need to go from there, cutting the list in half each time. The following should take 6 checks to find the correct level anywhere on the list.
local min, max = 1, LevelSystemSettings.MaxLevel + 1;
while min < max do
local mid = math.floor( ( min + max ) / 2 );
if XP < EXP[mid] then
max = mid - 1;
else
min = mid + 1;
end
end
LV = min - 1;
Im gonna check this now... I always appreciate your help Matt. And of course, I always try to improve my codes. Thanks you.