Xano
Xano

Reputation: 11

Is it possible to identify a table from a string variable?

I have this function that takes a string (s) and returns a table depending on what I entered.

function String2Faction(s)
    if s == "Nomads" then
        return Faction_Nomads
    elseif s == "Bandits" then
        return Faction_Bandits
    elseif s == "Military" then
        return Faction_Military
    elseif s == "WEM" then
        return Faction_WEM
    elseif s == "ECK" then
        return Faction_ECK
    end
end

Now, that's certainly a solution! But I was wondering if there was a different solution that wasn't tedious and annoying?

Edit, Solved: You can do everything above by simply writing

local options = {
    ["A"] = table1,
    ["B"] = table2,
    ["C"] = table3
}
return options[variable]

very easy

Upvotes: 1

Views: 26

Answers (0)

Related Questions