1
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
1
  • 1
    I don't understand exactly what you want to achieve? Do you want to use the names in the table to create functions that are globally and directly available without you knowing the name of it before? Commented Apr 14, 2021 at 8:15

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.