[1.1.32]Metatable doesn't work in multiplayer.

Bugs that are actually features.
Post Reply
yagaodirac
Fast Inserter
Fast Inserter
Posts: 151
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

[1.1.32]Metatable doesn't work in multiplayer.

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

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 4934
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

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

Post 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

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 1561
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

Post 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

yagaodirac
Fast Inserter
Fast Inserter
Posts: 151
Joined: Sun Jun 16, 2019 4:04 pm
Contact:

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

Post 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

Post Reply

Return to “Not a bug”