Context: There is a table like this
The indexes are the minimum level needed to achieve the rank, and the values in the table are tables with values inside themselves. A rank in this context is a string value which players can achieve as they progressively increase their levels.
1 | ranklist = { |
2 | [ 1 ] = Beginner -- These are all tables found elsewhere in the script |
3 | [ 10 ] = Experienced |
4 | [ 21 ] = Master |
5 | [ 43 ] = Boss |
6 | } |
I want to use this table in a way that with a given player's level, which is an integer value, I can extract the rank data tables.
In other words; Level 1 Player outputs the Beginner table; Level 9 Player outputs the Beginner table; Level 21 Player outputs the Master table; Level 25 Player outputs the Master table;
There are a lot of ways which this can be done but I like the linked list style as we can use the rank name as well as the level.
Example:-
01 | local rankList = { } |
02 |
03 | -- built in reverse order |
04 | rankList.Boss = { 43 , 'Boss' } |
05 | rankList.Master = { 21 , 'Master' , rankList.Boss } |
06 | rankList.Experienced = { 10 , 'Experienced' , rankList.Master } |
07 | rankList.Beginner = { 1 , 'Beginner' , rankList.Experienced } |
08 |
09 | local function getRank(rank, curRankName) |
10 | local rankName = rankList [ curRankName or 'Beginner' ] [ 2 ] -- the first rank |
11 | local nextRank = rankList [ curRankName or 'Beginner' ] [ 3 ] |
12 |
13 | -- loop and get next rank |
14 | while nextRank and rank > = nextRank [ 1 ] do |
15 | rankName = nextRank [ 2 ] |
This may be useful but it depends on how you want to use your rank table. I hope this helps.