Birk
Birk

Reputation: 211

Lua: define functions from a table

NativeTable = {
    ["print"] = {},
    ["LoadResourceFile"] = {}
}

for k, v in pairs(NativeTable) do
    k = function(...)
        print("test")
    end
end

this would result in defining them in the NativeTable and not as a global function

print = function(...)
    print("test")
end
LoadResourceFile = function(...)
    print("test")
end

So I'm trying to define a global function with a table name

Guess i could do smth like this But there must be a better way?

NativeTable = {
    ["test"] = {},
    ["LoadResourceFile"] = {}
}

local function OverWriteFunction(FuncName, Func)
    local Base = [[ = function(...)
    print("jaa")
end
    ]]
    local Final = FuncName .. Base
    return Final
end

for k, v in pairs(NativeTable) do
    load(OverWriteFunction(k))()
end

Upvotes: 1

Views: 369

Answers (2)

Piglet
Piglet

Reputation: 28940

This doesn't make too much sense for me:

NativeTable = {
    ["print"] = {},
    ["LoadResourceFile"] = {}
}

for k, v in pairs(NativeTable) do
    k = function(...)
        print("test")
    end
end

First you create a table with table elements print and LoadResourceFile.

Then you iterate over that table and replace the table elements with functions.

Why not simply do this:

myTable = {
  a = function() print("I'm function a") end,
  b = function() print("I'm function b") end,
}

or

myTable = {}
myTable.a = function () print("I'm function a") end

Then you can call the global function like so: myTable.a()

If you insist on having those functions outside of a table you can simply insert them into the global environment table.

for k, v in pairs(myTable) do _G[k] = v end

then you could call a and b globally. But whatever you're trying to accomplish there's probably a better way than this.

Upvotes: 0

Jelumar
Jelumar

Reputation: 510

In your first example you are redefining the variable k, that is local inside the for loop, so this will not be usable outside the one loop where you define it.

Your second example is something you absolutely should avoid if you can, since defining code inside a string and loading it later means, that even only a syntax error will not be shown on "compiling" it, but only when that exact part is executed. And especially, when you are concatenating string that are meant to be code, you just get the risk of glueing something together wrongly, if you try to have that made generically.

If I understood correctly what you are trying to achieve, I would say, it could be something like this:

NativeTable = {
    ["test"] = {},
    ["LoadResourceFile"] = {},
    [3] = {}
}

for k, v in pairs(NativeTable) do
    if type(k) == "string" then
        _G[k] = function(...)
            print("output")
        end
    end
end

test()
LoadResourceFile()
-- _G[3]()

Which outputs:

output
output

What I have done here is to use the _G table which is the global environment of lua, all things you define there with a string in the brackets will be globally available (and what is globally available is inside that table, so be careful, since you can override other functions and variables you have defined!), even when writing it as a normal function call. I did also make sure, to only do this, when the type of k was string, otherwise you could start to define functions that can be called like the last commented out call.

Upvotes: 2

Related Questions