public void evaluate() { // build unique function name based on GUID string funcName = "_" + _id.ToString().Replace("-", ""); // wrap user code in a function string funcCode = "function " + funcName + "()\n"; // add all local variables foreach (var kv in _localVariables) { funcCode += String.Format("local {0} = {1}\n", kv.Key, kv.Value); } funcCode += _code; funcCode += "\nend\n"; funcCode += funcName + "()"; // call the actual function // load function into lua and call it var luaFunc = _luaSession.loadString(funcCode, funcName); if (null == luaFunc) { return; } luaFunc.Call(); }
public bool evaluate() { // build unique function name based on GUID string funcName = "_" + _id.ToString().Replace("-", ""); // wrap user code in a function string funcCode = "function " + funcName + "()\n"; // add all local variables foreach (var kv in _localVariables) { funcCode += String.Format("local {0} = {1}\n", kv.Key, kv.Value); } funcCode += _code; funcCode += "\nend\n"; funcCode += "return " + funcName + "()"; // call and return the actual function // load function into lua and call it var luaFunc = _luaSession.loadString(funcCode, funcName); if (null == luaFunc) { return(false); } var result = luaFunc.Call(); // no data returned if (0 == result.Length) { return(false); } // attempt cast var asBool = result[0] as bool?; if (!asBool.HasValue) { Console.WriteLine("Condition (" + _id + ") returned a non-boolean type.\n" + _code); return(false); } return(asBool.Value); }
public List <Entity> evaluate() { // build unique function name based on GUID string funcName = "_" + _id.ToString().Replace("-", ""); // wrap user code in a function string funcCode = "function " + funcName + "()\n"; funcCode += _code; funcCode += "\nend\n"; funcCode += "return " + funcName + "()"; // call and return the actual function // load function into lua and call it var luaFunc = _luaSession.loadString(funcCode, funcName); if (null == luaFunc) { return(new List <Entity>()); } var result = luaFunc.Call(); // no data returned if (0 == result.Length) { return(new List <Entity>()); } // attempt cast to a single entity if (result[0] is Entity) { return(new List <Entity> { (Entity)result[0] }); } // attempt to cast to a list of entities if (result[0] is List <Entity> ) { return((List <Entity>)result[0]); } return(new List <Entity>()); }