• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

RevScripts alphabetical order

alcapone

Member
Joined
Jan 13, 2021
Messages
246
Reaction score
19
Lua:
    for i, town in ipairs(Game.getTowns()) do
        if not table.contains(blockedCitys, town:getName():lower()) then
             local name = string.format("%s", town:getName())
             local choice = window:addChoice(name)

how could i make him pull the cities in alphabetical order
I managed to do it, it just pulls the city list
 
Solution
the IDS of the cities appeared
Lua:
function Player.sendModalringvip(self)
    local cid = self:getId()
    local window = ModalWindow {
        title = "temples",
        message = "Which of the temples below would you like to be teleported to?",
    }

    local towns = {}
    for i, town in ipairs(Game.getTowns()) do
        towns[#towns + 1] = town:getName()
    end

    -- sort table alphabetically
    table.sort(towns)

    for i = 1, #towns do
        local name = towns[i]
        if not table.contains(blockedCitys, name:lower()) then
            window:addChoice(name)
        end
    end

    window:addButton("Go!",
        function(button, choice)
            local self = Player(cid)
            local town =...
My suggestion would be to sort the list before you addChoice

Example code
Lua:
local function sortAlphabetically(tableToBeSorted)
    return table.sort(tableToBeSorted, function(a, b) return a:lower() < b:lower() end)
end

local towns = {"thais", "carlin", "ab'dendriel", "venore", "kazordoon"}

for i = 1, #towns do
    print(towns[i])
end
print("---")

sortAlphabetically(towns)

for i = 1, #towns do
    print(towns[i])
end

Output
Code:
thais
carlin
ab'dendriel
venore
kazordoon
---
ab'dendriel
carlin
kazordoon
thais
venore
 
Basically this
Lua:
local cities = {}
for i, town in ipairs(Game.getTowns()) do
    cities[#cities+1] = town:getName()..i
end

-- sort table alphabetically
table.sort(cities)

local window = ModalWindow(1000, "Teleport", "Cities")
for i = 1, #cities do
    local id = tonumber(cities[i]:match("(%d+)"))
    local town = cities[i]:sub(1, #cities[i] - 1)
    window:addChoice(id, town)
end

On the creaturescript you can use the choiceId as town id
Lua:
local town = Town(choiceId)
if town then
    player:teleportTo(town:getTemplePosition())
end
 
-- sort table alpha

Basically this
Lua:
local cities = {}
for i, town in ipairs(Game.getTowns()) do
    cities[#cities+1] = town:getName()..i
end

-- sort table alphabetically
table.sort(cities)

local window = ModalWindow(1000, "Teleport", "Cities")
for i = 1, #cities do
    local id = tonumber(cities[i]:match("(%d+)"))
    local town = cities[i]:sub(1, #cities[i] - 1)
    window:addChoice(id, town)
end

On the creaturescript you can use the choiceId as town id
Lua:
local town = Town(choiceId)
if town then
    player:teleportTo(town:getTemplePosition())
end
1652494198159.png
the IDS of the cities appeared
 
the IDS of the cities appeared
Lua:
function Player.sendModalringvip(self)
    local cid = self:getId()
    local window = ModalWindow {
        title = "temples",
        message = "Which of the temples below would you like to be teleported to?",
    }

    local towns = {}
    for i, town in ipairs(Game.getTowns()) do
        towns[#towns + 1] = town:getName()
    end

    -- sort table alphabetically
    table.sort(towns)

    for i = 1, #towns do
        local name = towns[i]
        if not table.contains(blockedCitys, name:lower()) then
            window:addChoice(name)
        end
    end

    window:addButton("Go!",
        function(button, choice)
            local self = Player(cid)
            local town = Town(choice.text)
            if self and town then
                self:teleportTo(town:getTemplePosition())
            end
        end
    )
    window:setDefaultEnterButton("Go!")

    window:addButton("back",
        function(button, choice)
            local self = Player(cid)
            if self then
                self:sendMainModalringvip()
            end
        end
    )

    window:addButton("Exit")
    window:sendToPlayer(self)
end
 
Solution
Lua:
    -- sort table alphabetically
    table.sort(towns)
Just wanted to mention, that depending on how their table is setup, it's possible for the list to not be in alphabetical order using strictly table.sort as it's a lexical sort function.

Probably redundant in this situation, but good to keep in mind.

A quick example
Lua:
local towns = {"tha", "Thais", "thais"}

table.sort(towns)

for i = 1, #towns do
    print(towns[i])
end
output
Code:
Thais
tha
thais
 
Back
Top