neolithos / neolua

A Lua implementation for the Dynamic Language Runtime (DLR).
https://neolua.codeplex.com/
Apache License 2.0
471 stars 76 forks source link

How to call Lua function in my CSharp program? #128

Closed ghost closed 3 years ago

ghost commented 3 years ago

NeoLua Version:1.3.13

Example to reproduce:

public static Lua lua = new Lua();
public static LuaGlobal luaCE = lua.CreateEnvironment();
public static Dictionary<string, List<LuaChunk>> luaFuncs = new Dictionary<string, List<LuaChunk>>();
public static bool CallLuaFunc<T>(string key, T data)
{
    bool flag = true;
    if (luaFuncs.ContainsKey(key))
    {
      foreach (LuaChunk luaFunction in luaFuncs[key])
         {
            try
            {
                if (!Convert.ToBoolean(luaCE.DoChunk(~~data~~)[0] ?? true))
                {
                    flag = false;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"[ERROR] - {ex.Data}\n - Message:{ex.Message}");
                Console.ForegroundColor = ConsoleColor.White;
                return false;
            }
        }
    }
    return flag;
}
neolithos commented 3 years ago

Your example runs precompiled lua code. And it looks good so far. What is the problem?