/// <summary> /// Sets a field of a lua table to a lua variable identified by luaIdentifier i.e. luaTable.someField = luaVariable /// The table to operate on must currently be on top of the lua stack. /// After the call the lua table is still on top of the stack. /// </summary> /// <param name="fieldName">name of the field</param> /// <param name="luaIdentifier">the identifier identifying the lua variable</param> public void SetTableFieldToLuaIdentifier(string fieldName, string luaIdentifier) { Lua.lua_pushstring(L, fieldName); Lua.lua_getglobal(L, luaIdentifier); Lua.lua_settable(L, -3); //table is still on top of the stack }
/// <summary> /// Gets the value of a field of a table and returns it as type object i.e. return luaTable.someField /// After the call the lua stack is balanced /// </summary> /// <param name="tableName">name of lua table</param> /// <param name="fieldName">name of table field</param> /// <returns>returns luaTable.someField</returns> public object GetTableField(string tableName, string fieldName) { //push table on the stack Lua.lua_getglobal(L, tableName); if (IsTopNil) { throw new Exception("GetTableField, the table does not exist: " + tableName); } //push table field name on the stack Lua.lua_pushstring(L, fieldName); //get value of field of table: get tableName[fieldName] Lua.lua_gettable(L, -2); //get the result of the stack int luaType = 0; object value = GetValueOfStack(out luaType); //pop table of the stack Lua.lua_pop(L, 1); //stack is balanced return(value); }
/// <summary> /// Sets a field of a lua table to a lua variable identified by luaIdentifier i.e. luaTable.someField = luaVariable /// After the call the lua stack is balanced. /// </summary> /// <param name="tableName">name of the lua table</param> /// <param name="fieldName">name of the field</param> /// <param name="luaIdentifier">the identifier identifying the lua variable</param> public void SetTableFieldToLuaIdentifier(string tableName, string fieldName, string luaIdentifier) { Lua.lua_getglobal(L, tableName); Lua.lua_pushstring(L, fieldName); Lua.lua_getglobal(L, luaIdentifier); Lua.lua_settable(L, -3); Lua.lua_pop(L, 1); //stack is balanced }
/// <summary> /// Creates a table with tableName if it does not already exist. /// After creation or if the table already exists, stack will be empty and balanced. /// </summary> /// <param name="tableName"></param> public void CreateLuaTable(string tableName) { Lua.lua_getglobal(L, tableName); if (IsTopNil) { //remove the nil value Lua.lua_pop(L, 1); Lua.lua_newtable(L); Lua.lua_setglobal(L, tableName); //stack is empty } else { //remove the existing table from the stack Lua.lua_pop(L, 1); } }
/// <summary> /// Calls the Lua function 'luaFuncName' with the given parameters /// </summary> public void CallLuaFunction(string luaFuncName, object[] args) { Lua.lua_getglobal(L, luaFuncName); if (!Lua.lua_isfunction(L, -1)) { Lua.lua_pop(L, 1); return; } int argc = (args != null) ? args.Length : 0; for (int i = 0; i < argc; i++) { PushBasicValue(args[i]); } if (Lua.lua_pcall(L, argc, 0, 0) != 0) { m_errors.Add("Failed to run function '" + luaFuncName + "'"); return; } }
/// <summary> /// Sets a field of a lua table to a .net variable value i.e. luaTable.someField = value of .net variable. /// After the call the lua stack is balanced. /// </summary> /// <param name="tableName">name of the lua table</param> /// <param name="fieldName">name of the field</param> /// <param name="value">.net variable</param> public void SetTableField(string tableName, string fieldName, object value) { //push table on the stack Lua.lua_getglobal(L, tableName); if (IsTopNil) { throw new Exception("SetTableField, the table does not exist: " + tableName); } //push table field name on the stack Lua.lua_pushstring(L, fieldName); //push value on the stack PushBasicValue(value); //set field of table to value: tableName[fieldName]=value Lua.lua_settable(L, -3); //pop table of the stack Lua.lua_pop(L, 1); //stack is balanced }
/// <summary> /// Calls the Lua function 'luaFuncName' with the given parameters /// </summary> /// <param name="returnType">Lua type of the return value</param> public object CallLuaFunction(string luaFuncName, object[] args, out int luaType) { luaType = 0; Lua.lua_getglobal(L, luaFuncName); if (!Lua.lua_isfunction(L, -1)) { Lua.lua_pop(L, 1); return(null); } int argc = (args != null) ? args.Length : 0; for (int i = 0; i < argc; i++) { PushBasicValue(args[i]); } if (Lua.lua_pcall(L, argc, 1, 0) != 0) { m_errors.Add("Failed to run function '" + luaFuncName + "'"); return(null); } return(GetValueOfStack(out luaType)); }