Page 1 of 1

[0.15.37] Crash related to copying game objects

Posted: Tue Oct 31, 2017 4:52 pm
by blueblue
I accidentally tried to copy a table that has a player as a member (using a custom copy function). The game outputs an error into chat which is fine, then crashes which probably isnt. To reproduce in single-player 0.15.37:

Code: Select all

/c 
function copy(obj, seen)
    if type(obj) ~= 'table' then return obj end
    if seen and seen[obj] then return seen[obj] end
    local s = seen or {}
    local res = setmetatable({}, getmetatable(obj))
    s[obj] = res
    for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
    return res
end  
copy(game.player)

Re: [0.15.37] Crash related to copying game objects

Posted: Tue Oct 31, 2017 8:24 pm
by Rseding91
Thanks for the report. We don't support copying the meta table on a Factorio game reference. Doing so will result in a crash (or worse). At the moment I can't think of a simple solution that wouldn't have a performance overhead so for now I'm going to say "don't do that" :)

Re: [0.15.37] Crash related to copying game objects

Posted: Wed Nov 01, 2017 3:59 pm
by blueblue
I expected something like this, that's okay =)

Is there a simple way to distinguish between game references and normal tables so that I can amend my copy function to give me an error?

Re: [0.15.37] Crash related to copying game objects

Posted: Wed Nov 01, 2017 5:10 pm
by Nexela

Code: Select all

if type(val) == "table" and val.__self then --factorio object (unless of course you set __self on your tables in which case)
type(val.__self) evaluates to "userdata"

Re: [0.15.37] Crash related to copying game objects

Posted: Wed Nov 01, 2017 5:14 pm
by Rseding91
Nexela wrote:

Code: Select all

if type(val) == "table" and val.__self then --factorio object (unless of course you set __self on your tables)
If you do that (set __self) then you get zero support and I'll tell everyone to avoid that mod like the plague :P