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 6 years ago
Edited 6 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.

1ranklist = {
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;

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 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:-

01local rankList = {}
02 
03-- built in reverse order
04rankList.Boss = {43, 'Boss'}
05rankList.Master = {21, 'Master', rankList.Boss}
06rankList.Experienced = {10, 'Experienced', rankList.Master}
07rankList.Beginner = {1, 'Beginner', rankList.Experienced}
08 
09local 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]
View all 29 lines...

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 — 6y
Ad

Answer this question