This prints "0" to the console 10 times, then crashes.
Code: Select all
/c
local p = game.player
global.x = p.surface.create_entity{
name = 'steel-chest',
position = p.position,
force = p.force,
}
global.f = global.x.get_inventory(defines.inventory.chest).get_item_count
script.on_event(defines.events.on_tick, function()
print(global.f())
end)
Function references stored in global should stay usable for as long as the instance runs. When saving happens they should not be stored in the save.zip file, but remain in the lua state untouched. The mere act of storing them shouldn't crash the game :D.
Explanation
While trying to optimize performance I tried to cache the inventory methods directly in addition to caching the LuaInventory, expecting to be able to restore them in on_load. I tried caching them directly in a global subtable, and in an __index metatable, but both implementations crash. If saving happens before the crash the console *does* print a message that N functions have been removed from global as expected.
I've used get_item_count for the example, but I'm assuming this happens for other methods too.