Jump to content




Clone Tables


  • You cannot reply to this topic
2 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 11 July 2016 - 11:21 AM

_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?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 11 July 2016 - 12:17 PM

Check if tab == value. That way you know it's an __index.

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 11 July 2016 - 04:42 PM

I solved it, thanks.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users