Jonese1234
Jonese1234

Reputation: 323

Lua Table Function returning

So basically what I would like is to have is when you have a set of functions in a table, to be able to call it like print(timeRequirements[3]()) , to do the same as print(timeRequirements["old_man"]()

Here is my table code:

timeRequirements = {

        bulbasaur = function() --RESET
            if BEAST_MODE then
                return 1.99
            end
            return 2.22
    end,

    nidoran = function() --RESET
        if BEAST_MODE then
            return 6
        end
        return 6.4 + timeSaveFor("spearow")
    end,

    old_man = function()
        return 6.75 + timeSaveFor("spearow")
    end,

    forest = function()
        return 9.33 + timeSaveFor("spearow")
    end,

    brock = function()
        return 11 + timeSaveFor("spearow")
    end,

    shorts = function() --TWEET
        local timeLimit = 13.75 + timeSaveFor("spearow")
        timeLimit = timeLimit + (3 - stats.nidoran.rating) * 0.2
        return timeLimit
    end,

    mt_moon = function() --RESET
        if BEAST_MODE then
            return 24.75
        end

        local timeLimit = 25.50 + timeSaveFor("paras")
        if Pokemon.info("nidoking", "level") >= 18 then
            timeLimit = timeLimit + 0.33
        elseif Pokemon.getExp() > 3730 then
            timeLimit = timeLimit + 0.15
        end
        if stats.nidoran.attack > 15 then
            timeLimit = timeLimit + 0.25
        end
        if stats.nidoran.speed > 14 then
            timeLimit = timeLimit + 0.25
        end
        return timeLimit
    end,

    mankey = function()
        return 31.25 + timeSaveFor("paras")
    end,

    bills = function()
        return 36 + timeForStats() + timeSaveFor("paras")
    end,

    misty = function() --PB
        return 37.75 + timeForStats() + timeSaveFor("paras")
    end,

    vermilion = function()
        return 42.25 + timeForStats()
    end,

    trash = function() --RESET
        if BEAST_MODE then
            return 45.75
        end
        return 47.25 + timeForStats()
    end,

    safari_carbos = function()
        return 68.25 + timeForStats()
    end,

    victory_road = function() --PB
        return 97.3
    end,

    e4center = function()
        return 99.75
    end,

    blue = function()
        return 106.25
    end,

    champion = function() --PB
        return 112
    end,

}

It should return "6.75"

I don't really know how to do this, I tried a few things and all ended up returning

LuaInterface.LuaScriptException: [string "main"]:108: attempt to index field '?' (a nil value)

I would like to return the 3rd function in the tunnel.

Upvotes: 1

Views: 2450

Answers (2)

Renato Geh
Renato Geh

Reputation: 78

As an alternative to the hardcoded way of assigning each key an index, you could try getting all your keys from your function table ordered (note that maps are unordered in Lua; you'd have to use an array) and then assign each of them an index. This would, of course, mean that the indices would be lexicographically ordered (unless you make your own sorting function). Here's an example:

ftable = {
  name1 = function()
    return "name1 func"
  end,
  name2 = function()
    return "name2 func"
  end,
}

names = {}
for k, _ in pairs(ftable) do
  table.insert(names, k)
end
table.sort(names)

for i, name in ipairs(names) do
  ftable[i] = ftable[name]
end

print(ftable["name1"]())
print(ftable[1]())

Upvotes: 2

user94559
user94559

Reputation: 60133

You could add each function twice, once with a string key and once with a numeric key, like so:

-- Had to add this, since it's used in the old_man function
function timeSaveFor()
    return 0
end

timeRequirements = {}

timeRequirements.bulbasaur = function() --RESET
    if BEAST_MODE then
        return 1.99
    end
    return 2.22
end
timeRequirements[1] = timeRequirements.bulbasaur

timeRequirements.nidoran = function() --RESET
    if BEAST_MODE then
        return 6
    end
    return 6.4 + timeSaveFor("spearow")
end
timeRequirements[2] = timeRequirements.nidoran

timeRequirements.old_man = function()
    return 6.75 + timeSaveFor("spearow")
end
timeRequirements[3] = timeRequirements.old_man

print(timeRequirements["old_man"]()) -- 6.75
print(timeRequirements[3]()) -- 6.75

As an alternative, you could keep the definition of timeRequirements as-is and just add a table with the order you want. E.g.:

local order = { "bulbasaur", "nidoran", "old_man", "forest", "brock", "shorts", ... }

print(timeRequirements["old_man"]()) -- 6.75
print(timeRequirements[order[3]]()) -- 6.75

Either way, you need to somehow make explicit what order you think these things are in, because the keys of a table have no order.

Upvotes: 1

Related Questions