Page 1 of 1

[0.9.4] util.Table.DeepCopy() Corrupts entitys

Posted: Fri Mar 21, 2014 8:13 pm
by ludsoe
As nobody posted in this thread I'm re-purposing it, it turns out using table.deepcopy() on a table containing entity's causes the game to corrupt. I was able to get around the problem by writing my own table copy function that doesn't change meta tables. Using table.deepcopy() on entity's causes the game to crash.
OldStuff

Re: [0.9.4] util.Table.DeepCopy() Corrupts entitys

Posted: Mon Mar 24, 2014 12:02 pm
by slpwnd
Thanks for the report. The problem here is that the "factorio" lua objects (i.e. lua entity, lua technology) need to correspond to the C++ object in the backend. Factorio lua objects cannot be "raw-copied" (table copy of attributes). So the result of using deepcopy was there were two distinct tables that corresponded to one instanceĀ in the backend. When the original table (the one we copied from) is garbage collected (GC is explicitly triggered every 300ticks or so) then the object is deleted and the copy becomes invalidĀ (it tries to call functions on deleted C++ object).

For 0.9.5 I have changed the definition of deepcopy as follows:

Code: Select all

if type(object) ~= "table" then
  return object
-- presence of __self parameter means this is a factorio object
elseif object.__self then
  return object
elseif ...
This way the factorio objects will be treated as references within the deepcopy. This actually makes better sense because the entity in the game "cannot be copies", you can only make 2 distinct references.