Esempio n. 1
0
 /// <summary>
 /// Gets the value with the specified string 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 [string 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(Tools.StringToInt(key));
         }
         else
         {
             // Get dictionary value:
             LuaValue luaValueKey = luaTable.GetKey(key);
             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));
         }
     }
 }
Esempio n. 2
0
        //--- Unused now that methods are optimized to use LuaInterpreter directly.
        //private static Lua.Result SafeGetLuaResult(string luaCode) {
        //	try {
        //		return Lua.Run(luaCode, DialogueDebug.LogInfo);
        //	} catch (System.Exception) {
        //		return Lua.NoResult;
        //	}
        //}

        /// <summary>
        /// Checks if a table element exists.
        /// </summary>
        /// <returns><c>true</c>, if the table entry exists, <c>false</c> otherwise.</returns>
        /// <param name="table">Table name (e.g., "Actor").</param>
        /// <param name="element">Element name (e.g., "Player").</param>
        public static bool DoesTableElementExist(string table, string element)
        {
            LuaTable luaTable = Lua.Environment.GetValue(table) as LuaTable;

            return((luaTable != null) ? (luaTable.GetKey(StringToTableIndex(element)) != LuaNil.Nil) : false);
            //--- Was (unoptimized):
            //LuaTableWrapper luaTable = SafeGetLuaResult(string.Format("return {0}", new System.Object[] { table })).AsTable;
            //if (luaTable != null) {
            //	string tableIndex = StringToTableIndex(element);
            //	foreach (var o in luaTable.Keys) {
            //		if ((o.GetType() == typeof(string)) && (string.Equals((string) o, tableIndex))) return true;
            //	}
            //}
            //return false;
        }