LUA Utility Functions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Useful functions | Useful functions | ||
{{OldLua}} | |||
== Clone / Deep Copy == | == Clone / Deep Copy == | ||
Line 21: | Line 23: | ||
if key == "__metatable" then | if key == "__metatable" then | ||
-- do nothing, metatable is handled separately | -- do nothing, metatable is handled separately | ||
elseif type(value) == "table" then | elseif type(value) == "table" then | ||
target[key] = clone(value) | target[key] = clone(value) |
Latest revision as of 05:15, 18 July 2023
Useful functions
Warning: This page references the old LUA system and has not been updated yet for the new LUA system. Do not use this as a guide as this information will no longer work. |
Clone / Deep Copy
Useful for making a copy of the object. See the OOP example object for it in action.
function clone (t) -- deep-copy a table
if type(t) ~= "table" then return t end
local meta = getmetatable(t)
local target = {}
local keys = table.keys(t);
io.write("Keys array is: " .. type(keys) .. "\n");
io.write("Keys length is: " .. #keys .. "\n");
for i=1, #keys do
local key = keys[i];
local value = t[key];
io.write("Key: " .. key .. "\n");
if key == "__metatable" then
-- do nothing, metatable is handled separately
elseif type(value) == "table" then
target[key] = clone(value)
else
target[key] = value
end
end
setmetatable(target, meta)
return target
end