/// <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> /// Pushes the value on to the lua stack based on value.GetType(), and how that type maps to a lua type. /// Pushes nil if object is null or typeof object is not a basic type /// </summary> /// <param name="value">the value to push on to the stack</param> public void PushBasicValue(object value) { //push value on the stack if (value == null) { Lua.lua_pushnil(L); return; } if (value.GetType() == typeof(string)) { Lua.lua_pushstring(L, value.ToString()); return; } if (value.GetType() == typeof(bool)) { Lua.lua_pushboolean(L, (bool)value); return; } if (value.GetType() == typeof(decimal)) { Lua.lua_pushnumber(L, Convert.ToDouble(((decimal)value))); return; } if (value.GetType() == typeof(float)) { Lua.lua_pushnumber(L, Convert.ToDouble(((float)value))); return; } if (value.GetType() == typeof(double)) { Lua.lua_pushnumber(L, Convert.ToDouble(((double)value))); return; } if (value.GetType() == typeof(int)) { Lua.lua_pushinteger(L, (int)value); return; } if (value.GetType() == typeof(DateTime)) { Lua.lua_pushstring(L, ((DateTime)value).ToString()); return; } if (value.GetType() == typeof(Guid)) { Lua.lua_pushstring(L, ((Guid)value).ToString()); return; } if (value.GetType().IsEnum) { Lua.lua_pushstring(L, value.ToString()); return; } Lua.lua_pushnil(L); }
/// <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 table is still on top of the stack. /// </summary> /// <param name="fieldName">name of the field</param> /// <param name="value">.net variable</param> public void SetTableField(string fieldName, object value) { if (IsTopNil) { throw new Exception("SetTableField, no table on top of the stack"); } //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); //table is still on top of the stack }
/// <summary> /// Gets the value of a field of a lua table and returns it as type object i.e. return luaTable.someField /// 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"> /// the name of the field, belonging to a lua table currently sitting on /// top of the lua stack /// </param> /// <returns>returns luaTable.someField</returns> public object GetTableField(string fieldName) { if (IsTopNil) { throw new Exception("GetTableField, no table on top of stack"); } //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); //table is still on top of the stack return(value); }
/// <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 }