DynamicLua, Nlua vs Neolua, Is it better to use Neolua than these two ?

Nov 3, 2014 at 2:31 PM
  • I am new to Lua programming, and used Lua Inerface as of now and having problems in calling functions().
  • I want load some compiled Lua script from C#, after compiling the result needs to compared with defined output in lua script.
    Is this possible in Neolua. Please explain ?
Coordinator
Nov 3, 2014 at 8:36 PM
What do you want to do? I do not understand your question.
Nov 3, 2014 at 9:40 PM
Edited Nov 3, 2014 at 9:47 PM
I have seen your previous discussions but i am not clear with the differences between NLua, NeoLua, and Dynamic Lua Which one is better to use Lua from C# ?

In NeoLua is it possible to load compiled Lua script, Does NeoLua allow to use Lua from C# ?
Coordinator
Nov 3, 2014 at 10:08 PM
NLua and dynamiclua are basically the same, both projects using c-lua at the end. NeoLua implements the language from scratch on a totally different bases (the dlr).

In my opinion neolua is better to use with c# 😃 or the framework. Because there is no wrapper that has to translate all the objects, methods and so on.... And also the other way around it is very comfortable. It is easy to access/call member of lua tables, because the lua table is a .net class that is useable in every framework language (c#, vb.net, ironphython, ...). And the chunks or functions are only simple delegates.

NeoLua does not has any functionality to load compiled scripts, and I have currently no real reason to implement this feature. There are a lot ways to compile a script ones and run it multiple times in the memory. There is for me no need thing about a byte language.
Marked as answer by zulfihar_05 on 11/3/2014 at 3:03 PM
Nov 3, 2014 at 10:22 PM
Thank you so much for your answer. It was really helpful :) I would like to use NeoLua which is really nice which has more advantages as you said.
I have a function written to find the maximum number and length of the array in Lua ,
function maximum (a)
  local mi = 1          -- maximum index
  local m = a[mi]       -- maximum value
  for i,val in ipairs(a) do
    if val > m then
      mi = i
      m = val
    end
  end
  return m, mi
end
a = {5,8,4,9,2,3,10}
My question is how to call this function maximum() from using C# ?
Coordinator
Nov 3, 2014 at 10:43 PM
Edited Nov 3, 2014 at 10:45 PM
The same like you call it in c# ;)
Lua l;
...
LuaTable t = new LuaTable();
t[1] = 5; t[2] = 8; ...
dynamic g = l.CreateEnvironment();
g.dochunk("function maximum(a) ...", "test.lua");
LuaResult r = g.maximum(t);
Console.WriteLine("m={0},mi={1}", r[0], r[1]);
Or typed:
LuaGlobal g = l.CreateEnvironment();
g.DoChunk("...", "test.lua");

var maxi = (Func<object, LuaResult>)g["maximum"];
There are two more ways>
  1. As a chunk via l.CompileChunk
  2. Or you a compile a delegate, that not depends on a global environment. This is a nice feature for small formulas or functions they do not use any global lvariables.
Nov 4, 2014 at 2:07 PM
Thank you once again .
But I would like to call the function as like in LuaInterface, I define the functions and inputs in the script file and just call them in c#
for example :

//Lua script
function add(number1, number2)
return number1+number2
end
n1 = 2
n2 = 3

// C#
lua.DoFile("scripts\LuaAdd.lua");
LuaFunction luaFunc = lua.GetFunction("add");
double a = (double)lua["n1"];
double b = (double)lua["n2"];
if (luaFunc != null)
        {
            object[] objArr = luaFunc.Call(a,b);
            Console.WriteLine("The addition is: {0}", int.Parse(objArr[0].ToString()));
        }
Coordinator
Nov 4, 2014 at 2:53 PM
I would write it in this way:
LuaGlobal g = l.CreateEnvironment();
g.DoChunk("scripts\\LuaAdd.lua");
var luaFunc = g["add"] as Func<object, object, LuaResult>;
double a = Convert.ToDouble(g["n1"]);
double b = Convert.ToDouble(g["n2"]);
if (luaFunc != null)
{
  LuaResult r = luaFunc(a,b);
  Console.WriteLine("The addition is: {0}", r[0]);
}
Or in the dynmaic way, this should work too:
dynamic g = l.CreateEnvironment();
g.dochunk("scripts\\LuaAdd.lua");
dynamic luaFunc = g.add;
double a = g.n1;
double b = g.n2;
if (luaFunc != null)
{
  double r = luaFunc(a, b);
  Console.WriteLine("The addition is: {0}", r);
}
This is by the way a good example of the deep integration of NeoLua in the .net framework. It is no helper class LuaFunction visible.
Marked as answer by zulfihar_05 on 11/5/2014 at 6:17 AM
Nov 4, 2014 at 4:37 PM
Super :) that was really nice I expected the same :) Its working .
I have another question to get the values of the table :

// Lua script
table1 = { a1 = 2,
             a2 = 3,
             a3 = 4,
             a4 = 5,
             a5 = 6
    }
// in C#
LuaTable table4 = lua.GetTable("table1");
string[] arr = new string[] { table4["a1"].ToString(),
                                                   table4["a2"].ToString(), 
                                                   table4["a3"].ToString(), 
                                                   table4["a4"].ToString(),
                                                   table4["a4"].ToString()
                                                    };
I want use these tables in the maximum(a) function as you have written before :)
Coordinator
Nov 4, 2014 at 6:11 PM
It is also easy:
LuaTable table4 = (LuaTable)g["table1"];
string[] arr = new string[] { table4["a1"].ToString(), ... };
Dynamic:
dynamic table4 = g.table1;
string[] arr = new string[] { table4.a1, table4.a2, table4["a3"], ... };
Interesting are also: table4.Members
Marked as answer by zulfihar_05 on 11/6/2014 at 7:32 AM
Nov 6, 2014 at 3:22 PM
Edited Nov 6, 2014 at 3:46 PM
Thank you so much for your answer it was simple and really easy. have an another question with the tables :

//in Lua

test={}
test[1] = 1
test[2] = 0
test[3] = 0
test[4] = 1
test[5] = 0
test[6] = 0

I want to get the length of the array : test={} .
In Lua i give it like this print(table.getn(test))
As of now I am trying to save them to a variable like -- length = table.getn(test)
Can you help me if we can get directly in C# ?

LuaTable test = (LuaTable)g["test"];
......
Coordinator
Nov 6, 2014 at 7:57 PM
Do it just in the .net way:
((LuaTable)g["test"]).Length;
Enjoy.
Nov 18, 2014 at 10:54 AM
Edited Nov 18, 2014 at 10:55 AM
Hello, I have a question again :

My Lua script has two files,one master file and sub file. The master file calls the subfiles by using dofile(".......")
I want to call the Master file from C# and get the table values of subfile :)
I have put the example below:

Masterlua.lua
dofile("Phasechange.lua")
print(array[1])
print("The length of the array is :",table.getn(array))

Phasechange.lua

array={}
count = 1;

function Phasechange(value)
array[count] = value
count = count + 1
end

Phasechange(4)
Phasechange(3)
Phasechange(2)
Phasechange(1)
Phasechange(0)

return

So my question is when i want to call the table array = {} from C#,
LuaTable table4 = (LuaTable)g["array"];
string[] arr = new string[] { table4[1].ToString(), ... };
Console.WriteLine(Int32.Parse(Phase[0]));

I get an error : attempt to index global 'array' (a nil value). kindly help me ?
Coordinator
Nov 18, 2014 at 11:26 AM
Edited Nov 18, 2014 at 11:26 AM
Hello,

it would be nice, that you create for new question a new thread :).

So, I double checked:

m.lua
dofile([[d:\temp\a\p.lua]]) -- changed path
print(array[1])
print("The length of the array is :", #array); -- changed getn to official lua syntax (were comes the getn from, I do not find this in the documentation)
p.lua (nothing changed)
array={}
count = 1;

function Phasechange(value)
array[count] = value
count = count + 1
end

Phasechange(4)
Phasechange(3)
Phasechange(2)
Phasechange(1)
Phasechange(0)
The c# program:
using (Lua l = new Lua())
{
  dynamic g = l.CreateEnvironment();

  g.dofile(@"d:\temp\a\m.lua");

 LuaTable t = (LuaTable)g.array;
  Console.WriteLine("{0}", t[1]);
}
No, exceptions. Please, check your NeoLua version. I believe, that the exception is thrown in the master script. When this is true, the script is not executed? There was a bug in earlier versions.
Marked as answer by zulfihar_05 on 11/18/2014 at 4:39 PM