Page 1 of 1

[1.1.32]Metatable doesn't work in multiplayer.

Posted: Tue Apr 20, 2021 7:17 am
by yagaodirac
It seems that metatable feature doesn't function in multiplayer.

Code: Select all

global = global or {}
global.mt = global.mt or {}
global.mt.array_simple = {}
global.mt.array_simple.__newindex = function()end
global.mt.array_simple.__index = global.mt.array_simple
function global.mt.array_simple:add(element)
	self.length = self.length +1
	rawset(self, self.length, element)
end

global.new = global.new or {}
global.new.array_simple = function()
	local self = {}
	self.length = 0
	setmetatable(self, global.mt.array_simple)
	return self
end
Then, inside the control.lua

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	if not flag
	then 
		flag = true
		local cont = global.new.array_simple()
		cont:add({})--this line causes error in multiplayer. Error is like, add is nil.
	end
end)
This works properly in single player. But when it runs on a server, the add function is always nil.

Re: [1.1.32]Metatable doesn't work in multiplayer.

Posted: Tue Apr 20, 2021 8:21 am
by Klonan
Meta tables are not saved and loaded, you need to reset them using the on_load event, example mod:
https://github.com/Klonan/Mining_Drones ... e.lua#L570

Re: [1.1.32]Metatable doesn't work in multiplayer.

Posted: Tue Apr 20, 2021 10:03 am
by boskid
point 6 in https://lua-api.factorio.com/latest/Data-Lifecycle.html

Anything from point 3 to the end may be interesting to you

Re: [1.1.32]Metatable doesn't work in multiplayer.

Posted: Wed Apr 21, 2021 6:48 am
by yagaodirac
Klonan wrote: Tue Apr 20, 2021 8:21 am
boskid wrote: Tue Apr 20, 2021 10:03 am
Thank both of you.
It's here:
https://mods.factorio.com/mod/Multiplay ... OP-example