Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How do I organize this list of rank titles?

Asked by 5 years ago
Edited 5 years ago

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.


ranklist = { [1] = Beginner -- These are all tables found elsewhere in the script [10] = Experienced [21] = Master [43] = Boss }

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;

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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:-

local rankList = {}

-- built in reverse order 
rankList.Boss = {43, 'Boss'}
rankList.Master = {21, 'Master', rankList.Boss}
rankList.Experienced = {10, 'Experienced', rankList.Master}
rankList.Beginner = {1, 'Beginner', rankList.Experienced}

local function getRank(rank, curRankName)
    local rankName = rankList[curRankName or 'Beginner'][2] -- the first rank
    local nextRank = rankList[curRankName or 'Beginner'][3]

    -- loop and get next rank
    while nextRank and rank >= nextRank[1] do
        rankName = nextRank[2]
        nextRank = nextRank[3]
    end

    return rankName
end

-- using no rank name
for i=1, 50 do
    print('rank for ', i, ' is ', getRank(i))
end

-- with rank name
-- jump to that rank name then level from that rank 
print('rank with name ', getRank(250, 'Experienced'))

This may be useful but it depends on how you want to use your rank table. I hope this helps.

0
Good approach! For clarity I think it can be a good idea to define the next node under the next key in a dictionary, and the value under a "value" key. Avigant 2374 — 5y
Ad

Answer this question