Переглянути джерело

修改table.find()函数 用于适配targetMode

mafei 1 рік тому
батько
коміт
bca005de5f
1 змінених файлів з 32 додано та 1 видалено
  1. 32 1
      script/common/Util.lua

+ 32 - 1
script/common/Util.lua

@@ -552,7 +552,6 @@ table.find = function(array,elem)
 			return idx
 		end
 	end
-	return #array + 1
 end
 
 --------------------------------------------------------
@@ -568,6 +567,38 @@ table.shuffle = function(array)
 	return array
 end
 
+--------------------------------------------------------
+--- 新增table.copy函数
+--- @param1 array 数组
+--------------------------------------------------------
+
+function table.copy(object)
+    -- 已经复制过的table,key为复制源table,value为复制后的table
+    -- 为了防止table中的某个属性为自身时出现死循环
+    -- 避免本该是同一个table的属性,在复制时变成2个不同的table(内容同,但是地址关系和原来的不一样了)
+    local lookup_table = {}
+    local function _copy(object)
+        if type(object) ~= 'table' then -- 非table类型都直接返回
+            return object
+        elseif lookup_table[object] then
+            return lookup_table[object]
+        end 
+        local new_table = {}
+        lookup_table[object] = new_table
+        for k,v in pairs(object) do
+            new_table[_copy(k)] = _copy(v) 
+        end 
+        -- 这里直接拿mt来用是因为一般对table操作不会很粗暴的修改mt的相关内容
+        return setmetatable(new_table, getmetatable(object))
+    end 
+    return _copy(object)                    
+end
+
+--------------------------------------------------------
+--- 新增table.print_lua_table函数
+--- @param1 array 数组
+--------------------------------------------------------
+
 local function print_lua_table(lua_table, indent)
 	indent = indent or 0
 	for k, v in pairs(lua_table) do