RevScripts alphabetical order

alcapone

Member
Joined
Jan 13, 2021
Messages
152
Reaction score
12
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 =...

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
6,036
Solutions
493
Reaction score
4,207
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
 

Roddet

life.rbegin()
Joined
May 1, 2013
Messages
622
Solutions
57
Reaction score
342
Location
MX
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
 
OP
OP
A

alcapone

Member
Joined
Jan 13, 2021
Messages
152
Reaction score
12
-- 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
 

Roddet

life.rbegin()
Joined
May 1, 2013
Messages
622
Solutions
57
Reaction score
342
Location
MX
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

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
6,036
Solutions
493
Reaction score
4,207
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
 
Top