/// <summary> /// Sets the value of a field in a Lua table element. /// </summary> /// <param name="table">Table name.</param> /// <param name="element">Name of an element in the table.</param> /// <param name="field">Name of a field in the element.</param> /// <param name="value">The value to set the field to.</param> public static void SetTableField(string table, string element, string field, object value) { Lua.WasInvoked = true; LuaTable luaTable = Lua.Environment.GetValue(table) as LuaTable; if (luaTable == null) { throw new System.NullReferenceException("Table not found in Lua environment: " + table); } LuaTable elementTable = luaTable.GetValue(StringToTableIndex(element)) as LuaTable; if (elementTable == null) { Lua.Run(string.Format("{0}[\"{1}\"] = {{}}", table, StringToTableIndex(element))); elementTable = luaTable.GetValue(StringToTableIndex(element)) as LuaTable; if (elementTable == null) { throw new System.NullReferenceException("Unable to find or add element: " + element); } } elementTable.SetNameValue(StringToTableIndex(field), LuaInterpreterExtensions.ObjectToLuaValue(value)); //--- Was (unoptimized): //if (DoesTableElementExist(table, element)) { // bool isString = (value != null) && (value.GetType() == typeof(string)); // string valueInLua = isString // ? DoubleQuotesToSingle((string) value) // : ((value != null) ? value.ToString().ToLower() : "nil"); // Lua.Run(string.Format("{0}[\"{1}\"].{2} = {3}{4}{3}", // new System.Object[] { table, // StringToTableIndex(element), // StringToTableIndex(field), // (isString ? "\"" : string.Empty), // valueInLua }), // DialogueDebug.LogInfo); //} else { // if (DialogueDebug.LogWarnings) { // Debug.LogWarning(string.Format("{0}: Entry \"{1}\" doesn't exist in table {2}[]; can't set {3} to {4}", // new System.Object[] { DialogueDebug.Prefix, StringToTableIndex(element), table, field, value })); // } //} }
/// <summary> /// Sets the value of a variable in the Lua Variable table. /// </summary> /// <param name="variable">Variable name.</param> /// <param name="value">Value to set the variable to.</param> /// <example><code>SetVariable("Met_Wizard", true);</code></example> public static void SetVariable(string variable, object value) { Lua.WasInvoked = true; LuaTable luaTable = Lua.Environment.GetValue("Variable") as LuaTable; if (luaTable == null) { return; } luaTable.SetNameValue(StringToTableIndex(variable), LuaInterpreterExtensions.ObjectToLuaValue(value)); //--- Was (unoptimized): //if (string.IsNullOrEmpty(variable)) return; //bool isString = (value != null) && (value.GetType() == typeof(string)); //string valueInLua = isString // ? DoubleQuotesToSingle((string) value) // : ((value != null) ? value.ToString().ToLower() : "nil"); //Lua.Run(string.Format("Variable[\"{0}\"] = {1}{2}{1}", // new System.Object[] { StringToTableIndex(variable), // (isString ? "\"" : string.Empty), // valueInLua }), // DialogueDebug.LogInfo); }
/// <summary> /// Gets the value with the specified int key. Returns a standard type such as /// <c>string</c>, <c>float</c>, <c>bool</c>, or <c>null</c>. If the value /// is a table, it returns a LuaTableWrapper around it. /// </summary> /// <param name="key">Key.</param> public object this[int key] { get { if (luaTable == null) { if (DialogueDebug.logErrors) { Debug.LogError(string.Format("{0}: Lua table is null; lookup[{1}] failed", new object[] { DialogueDebug.Prefix, key })); } return(null); } LuaValue luaValue = LuaNil.Nil; if (luaTable.Length > 0) { // Get list value: luaValue = luaTable.GetValue(key); } else { // Get dictionary value: LuaValue luaValueKey = luaTable.GetKey(key.ToString()); if (luaValueKey == LuaNil.Nil) { //--- Suppressed: if (DialogueDebug.LogErrors) Debug.LogError(string.Format("{0}: Lua table does not contain key [{1}]", new string[] { DialogueDebug.Prefix, key })); return(null); } luaValue = luaTable.GetValue(key); } if (luaValue is LuaTable) { return(new LuaTableWrapper(luaValue as LuaTable)); } else { return(LuaInterpreterExtensions.LuaValueToObject(luaValue)); } } }