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

This subforum contains all the issues which we already resolved.
User avatar
ludsoe
Fast Inserter
Fast Inserter
Posts: 245
Joined: Tue Feb 11, 2014 8:16 am
Contact:

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

Post 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
slpwnd
Factorio Staff
Factorio Staff
Posts: 1835
Joined: Sun Feb 03, 2013 2:51 pm
Contact:

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

Post 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.
Post Reply

Return to “Resolved Problems and Bugs”