示例#1
0
 /// <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);
     }
 }
示例#2
0
        /// <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;
            }
        }
示例#3
0
        /// <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
        }
示例#4
0
        /// <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));
        }
示例#5
0
        /// <summary>
        /// Pops the value sitting on top of the lua stack
        /// </summary>
        /// <param name="luaType">the lua type of the value that was retrieved from the stack, is stored in this out parameter</param>
        /// <returns>the value popped off the stack</returns>
        public object GetValueOfStack(out int luaType)
        {
            luaType = 0;
            object value = null;

            switch (Lua.lua_type(L, -1))
            {
            case Lua.LUA_TNONE: {
                value   = null;
                luaType = Lua.LUA_TNONE;
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            case Lua.LUA_TNIL: {
                value   = null;
                luaType = Lua.LUA_TNIL;
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            case Lua.LUA_TSTRING: {
                luaType = Lua.LUA_TSTRING;
                value   = Lua.lua_tostring(L, -1);
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            case Lua.LUA_TNUMBER: {
                luaType = Lua.LUA_TNUMBER;
                value   = Lua.lua_tonumber(L, -1);
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            case Lua.LUA_TBOOLEAN: {
                luaType = Lua.LUA_TBOOLEAN;
                int intToBool = Lua.lua_toboolean(L, -1);
                value = intToBool > 0 ? true : false;
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            case Lua.LUA_TTABLE: {
                luaType = Lua.LUA_TTABLE;
                value   = "table";
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

            default: {
                value = null;
                //pop value from stack
                Lua.lua_pop(L, 1);
                break;
            }

                //case Lua.LUA_TINTEGER:
                //{
                //    value = Lua.lua_tointeger(m_lua_State, -1);
                //    //pop value from stack
                //    Lua.lua_pop(m_lua_State, 1);
                //    break;
                //}
            }

            return(value);
        }