From 1e389f8097eaf9a11f46108592f30a547009408b Mon Sep 17 00:00:00 2001 From: shagu Date: Fri, 23 Dec 2016 19:06:30 +0100 Subject: [PATCH] functions: add function to copy tables --- env/functions.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/env/functions.lua b/env/functions.lua index bd981474..68285529 100644 --- a/env/functions.lua +++ b/env/functions.lua @@ -35,3 +35,21 @@ function CreateGoldString(money) return string end + +function CopyTable(src) + local lookup_table = {} + local function _copy(src) + if type(src) ~= "table" then + return src + elseif lookup_table[src] then + return lookup_table[src] + end + local new_table = {} + lookup_table[src] = new_table + for index, value in pairs(src) do + new_table[_copy(index)] = _copy(value) + end + return setmetatable(new_table, getmetatable(src)) + end + return _copy(src) +end \ No newline at end of file