How to register a C # class constructor in Lua
I am using C # class:
public class TestClass
{
int _a;
public void Set(int a)
{
_a = a;
}
public void Print()
{
Console.WriteLine(_a);
}
}
and register it:
Lua lua = new Lua();
lua["Debug"] = new TestClass();
lua.DoFile("script.lua");
and call it from script like this:
a=Debug
a:Set(5)
a:Print()
What should I change / add to use the parameterized constructor?
+3
source to share
1 answer
First, you need to import the appropriate namespace your class is TestClass
in in order to use it from a lua script:
namespace Application
{
public class TestClass
{
int _a;
public void Print()
{
Console.WriteLine(_a);
}
public TestClass(int a)
{
this._a = a;
}
}
}
Lua lua = new Lua();
lua.LoadCLRPackage();
lua.DoFile("script.lua");
You should now be able to instantiate TestClass
from the script.lua file:
import ('Application')
a=TestClass(5)
a:Print()
+1
source to share