I use C # class:

public class TestClass { int _a; public void Set(int a) { _a = a; } public void Print() { Console.WriteLine(_a); } } 

I register it:

 Lua lua = new Lua(); lua["Debug"] = new TestClass(); lua.DoFile("script.lua"); 

And call it from the script:

 a=Debug a:Set(5) a:Print() 

What do I need to add or change to use a constructor with parameters?

1 answer 1

You must first import the corresponding namespace in which your TestClass class is TestClass to use it in the 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"); 

Script file code.lua:

 import ('Application') a=TestClass(5) a:Print() 

Source: my answer to enSO