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

Sorting a table of functions by index, but how?

Asked by 3 years ago

I would like to make a table of functions. Functions have their indexes represented as a level id (e.g. 100, 101, 200, 201, 202...) to change some level properties (lighting, gravity ect) So here is an example:

local functionsTable = {}

functionsTable[100] = function()
    print("This is a function number 100")
end

functionsTable[101] = function()
    print("This is a function number 101")
end

functionsTable[201] = function()
    print("This is a function number 201")
end

You might ask, what's the problem? And a problem comes whenever a player connects to a server, and to apply all property changes in the past, my client script loops trough all of the functions in a table until it reaches current player's level. For example:

local functionsTable = {}
local plrLevelId = 200

functionsTable[100] = function()
    print("This is a function number 100")
end

functionsTable[101] = function()
    print("This is a function number 101")
end

functionsTable[201] = function()
    print("This is a function number 201")
end

for i, v in pairs(functionsTable) do
    print("Index " .. tostring(i))
    if i <= plrLevelId then
        functionsTable[i]() -- calling a function of previous levels to apply changes
    else
        print("Skipped " .. tostring(i) .. " - done")
        break -- all functions of the previous and current level have been applied
    end
end

--[[
Output:
    Index 101
    This is a function number 101
    Index 201
    Skipped 201 - done

Expected output:
    Index 100
    This is a function number 100
    Index 101
    This is a function number 101
    Index 201
    Skipped 201 - done
]]

Not only it skips a function that supposed to called in the order, but it called a function number 101, while function number 100 should be called first. What magic is this? How to go through all functions in a correct order that I assigned by index? Thank you in advance

1 answer

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

pairs loops in randomized order, thus it can sometime loop through 201 first but sometime through 101 first. Your solution is ipairs which goes strictly by order, what you do is get all the indices, sort them and loop through them. Since ipairs loops by incrementing the index by one, it can't loop 101 -> 200, it will break instantly, it is expected to loop like 1 -> 2 -> 3 -> 4 ..., that's the reason you create list of indices.

local functionsTable = {}
local plrLevelId = 200

functionsTable[100] = function()
  print("This is a function number 100")
end

functionsTable[101] = function()
  print("This is a function number 101")
end

functionsTable[201] = function()
  print("This is a function number 201")
end

local function getIndices(tbl)
  local indices = {}

  for i in pairs(tbl) do
    table.insert(indices, i)
  end

  return indices
end

local indices = getIndices(functionsTable)
table.sort(indices)

for _, i in ipairs(indices) do
  print("Index " .. i)
  if i <= plrLevelId then
    functionsTable[i]() 
  else
    print("Skipped " .. i .. " - done")
    break
  end
end

about ipairs

about table.sort

ipairs vs pairs

0
Very interesting solution, thank you MrSuperKrut 167 — 3y
Ad

Answer this question