This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Clone Tables

Started by Sewbacca, 11 July 2016 - 10:21 AM
Sewbacca #1
Posted 11 July 2016 - 12:21 PM

_G.table['clone'] = {}
_G.table['clone']['table'] = function(tab, excludes)
  local cloneTable = {}
  excludes = excludes or {}
 
  for key, value in pairs(tab) do
    if type(value) == 'table' then
	  if not excludes[value] then
	    excludes[value] = cloneTable[key]
	    cloneTable[key] = table.clone(value, excludes) -- Endless loop, by _G._G -> _G._G._G .... _G._G._G._G._G.ERROR
	  else
	    cloneTable[key] = excludes[value]
	  end
    elseif type(value) == 'function' then
	  cloneTable[key] = function(...) return value(...) end
    else
	  cloneTable[key] = value
    end
  end
 
  return cloneTable
end

setmetatable(_G.table['clone'], {__call = function(_, ...)
    return _G.table['clone']['table'](...)
  end, __metatable = 'attempt to acess a protected metatable'})

It loops endless, by cloning _G, how can I prevent it?
KingofGamesYami #2
Posted 11 July 2016 - 01:17 PM
Check if tab == value. That way you know it's an __index.
Sewbacca #3
Posted 11 July 2016 - 05:42 PM
I solved it, thanks.