2

I am using NLua for script interface with my application if my application takes multiple files such as one.lua and two.lua

i want to get all the functions in all files in to a list of luafunctions

List<LuaFunctions> Functions;

NLua doesnt seem to have such a feature, but is there a way around it, there is a GetFunction(string) methodthat will return the function that you named, i can ofc do a brute force method on the GetFunction method but that will make my application take hours to start up with.

Any ways to work around this and get all functions in all files to a list of luafunctions?

  • Iterate through `Lua.Globals` and filter on the values that are functions. –  Mar 16 '14 at 22:49
  • Functions are values. Values don't exist until the code that creates them is executed. To get a list of all live values that are functions at a point during execution, you'd have to walk the whole object graph. You could inspect the Lua bytecode to see where a function might get created and the source location of its function definition (if it hasn't been stripped out). Or, you could inspect Lua source code for expressions that define functions. But those aren't not the same thing a function value. Is this an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – Tom Blodget Mar 17 '14 at 02:31
  • na, don't think it originally fell on XY but i seemed to have found a work around a couple of hours later by listing them in a table and then getting the table content list. Its not the result i originally wanted but it works. – Theodor Solbjørg Mar 18 '14 at 09:54

1 Answers1

1

As functions cannot be listed out of the blue, i found another way around it a couple of hours later.

i listed all functions on a table. so my lua code:

function Start()
   // something
end

function Update()
   // something else
end

became this:

var main = {}

function main.Start()
   // something
end

function main.Update()
   // something else
end

that way i could take them from a table listing, using

lua.GetTable({tablename});

which i have written a requirement for has to be named the same as the file so it would become:

var funcList = lua.GetTable(Path.GetFileNameWithoutExtension(c:\main.lua));

that would take and list all functions and then we can use:

lua.GetFunction(funcList[0]).Call();

as an example. Took me a while to find this work-around and i hope it will benefit someone.

  • 1
    You could have all such a.lua and b.lua files return their "main" table. Every file is compiled as a function so it can have return statements. There is no need for any globals; make "main" local. Also, look at the way Lua modules are created. – Tom Blodget Mar 18 '14 at 23:03