Example #1
0
        /// <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 }));
            //	}
            //}
        }
Example #2
0
        /// <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);
        }