Reputation: 3111
I'm just learning about Lua and trying to integrate it with C# and mono (on Linux). After some looking around, I found AluminumLua as a wrapper to do so.
I've successfully being able to call from lua to C#, but I can't see the way to call from C# to lua:
lua (test.lua):
HelloWorld()
function print_test()
print("hi")
return 1
end
C#
var context = new LuaContext ();
context.AddBasicLibrary ();
context.AddIoLibrary ();
context.SetGlobal ("HelloWorld", LuaObject.FromDelegate(new Action(HelloWorld)));
var parser = new LuaParser (context, "test.lua");
parser.Parse ();
...
public static void HelloWorld() {
Console.Write("HelloWorld");
}
That's cool, but... How can I call the function "print_test", get its output result from C#?
Upvotes: 1
Views: 318
Reputation: 20838
From looking at the source, LuaContext
has a Get
method which returns a LuaObject
. After you have a reference to that LuaObject
you can try to turn it into a LuaFunction
using AsFunction
and IsFunction
.
Something along the lines of this should work:
// ...
var print_test = context.Get("print_test");
if (print_test.IsFunction)
{
print_test.AsFunction()(null);
}
else
{
Console.Write("print_test not a lua function!");
}
// ...
Upvotes: 1