Skip to content

Commit 6a43b3a

Browse files
SmallJokerZeno-
authored andcommittedNov 27, 2014
Add minetest.copy_table(table) To get rid off the "table references"
Signed-off-by: Craig Robbins <kde.psych@gmail.com>
1 parent 77137a9 commit 6a43b3a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed
 

‎builtin/common/misc_helpers.lua

+12
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,18 @@ function core.pos_to_string(pos)
490490
return "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ")"
491491
end
492492

493+
--------------------------------------------------------------------------------
494+
function table.copy(t, seen)
495+
local n = {}
496+
seen = seen or {}
497+
seen[t] = n
498+
for k, v in pairs(t) do
499+
n[type(k) ~= "table" and k or seen[k] or table.copy(k, seen)] =
500+
type(v) ~= "table" and v or seen[v] or table.copy(v, seen)
501+
end
502+
return n
503+
end
504+
493505
--------------------------------------------------------------------------------
494506
-- mainmenu only functions
495507
--------------------------------------------------------------------------------

‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,8 @@ minetest.is_yes(arg)
13101310
^ returns whether arg can be interpreted as yes
13111311
minetest.get_us_time()
13121312
^ returns time with microsecond precision
1313+
table.copy(table) -> table
1314+
^ returns a deep copy of a table
13131315

13141316
minetest namespace reference
13151317
-----------------------------

1 commit comments

Comments
 (1)

HybridDog commented on Nov 27, 2014

@HybridDog
Contributor

there's a way to do it without recursion

Please sign in to comment.