Call a method in C# from Lua

Nov 26, 2014 at 3:40 PM
Edited Nov 27, 2014 at 8:55 AM
HI ,

I would like to call a C# method(SCS_cs) from Lua which returns a value. is it possible?
I have put a sample code,
--Lua : 
--  '_cs' is  call C# method ; '_f' is a Lua function;
function SetSCSPhase_f(lastphase)
         for i = 1, 3 do
            print("The value returned is :", lastphase, ".");
         end
                if not SCS_cs(4) then
                     print("the returned value is ", SCS_cs() );
                     return false
                end
end
// C#

     private int SCS_cs(int number)
        {  Console.WriteLine("The value is " + number);        
            ......
            ......
            ....    
            return 1;
        }
private void Form1_Load(object sender, EventArgs e)
        {   using (Lua l = new Lua())
            {   LuaGlobal g = l.CreateEnvironment();
                g.DoChunk("C:\\Users\\AVZ1LO\\Desktop\\luatest\\EA\\EA\\script\\Test.lua");
                var luaFunc = g[" SetSCSPhase_f"] as Func<object, LuaResult>;
                if (luaFunc != null)
                {     LuaResult r = luaFunc(5);                     
                }
}
My aim would be like to call some Lua functions from C# and inside the lua functions they would call another methods from C# and return values to lua. Finally lua tells C# to print the returned values to C# !  
Thanks :)
Coordinator
Nov 26, 2014 at 4:35 PM
That is what I lua made for :).

Simple assign a member to the global table.
private void Form1_Load(object sender, EventArgs e)
{
  using (Lua l = new Lua())
  {
      LuaGlobal g = l.CreateEnvironment();
      g.DoChunk("C:\\Users\\AVZ1LO\\Desktop\\luatest\\EA\\EA\\script\\Test.lua");

      g["SCS_cs"] = new Func<int, int>(SCS_cs);

      var luaFunc = g[" SetSCSPhase_f"] as Func<object, LuaResult>;
      if (luaFunc != null)
      {
         LuaResult r = luaFunc(5);                     
      }
}
Marked as answer by zulfihar_05 on 12/17/2014 at 4:46 AM