Skip to content

Commit

Permalink
Add minetest.copy_table(table) To get rid off the "table references"
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Robbins <kde.psych@gmail.com>
  • Loading branch information
SmallJoker authored and Zeno- committed Nov 27, 2014
1 parent 77137a9 commit 6a43b3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions builtin/common/misc_helpers.lua
Expand Up @@ -490,6 +490,18 @@ function core.pos_to_string(pos)
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
end

--------------------------------------------------------------------------------
function table.copy(t, seen)
local n = {}
seen = seen or {}
seen[t] = n
for k, v in pairs(t) do
n[type(k) ~= "table" and k or seen[k] or table.copy(k, seen)] =
type(v) ~= "table" and v or seen[v] or table.copy(v, seen)
end
return n
end

--------------------------------------------------------------------------------
-- mainmenu only functions
--------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1310,6 +1310,8 @@ minetest.is_yes(arg)
^ returns whether arg can be interpreted as yes
minetest.get_us_time()
^ returns time with microsecond precision
table.copy(table) -> table
^ returns a deep copy of a table

minetest namespace reference
-----------------------------
Expand Down

1 comment on commit 6a43b3a

@HybridDog
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a way to do it without recursion

Please sign in to comment.