Multiplayer Desync on Item Creation

Place to get help with not working mods / modding interface.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Multiplayer Desync on Item Creation

Post by AenAllAin »

I wrote a pretty simple mod of a wall with an invisible electric pole as a helper entity. I got it to work in single-player just fine; however, when I play multiplayer the game desyncs every time one of the item pairs is placed. I was hoping someone would know a very basic multiplayer mistake I am making creating additional items in the "on_built_entity" event handler ...if not, I can post the code and the logs. Thanks.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by prg »

Having the actual code to look at instead of making wild guesses would be useful.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

prg wrote:Having the actual code to look at instead of making wild guesses would be useful.
I'll post the code when I get home ...but like I said, I was hoping there was a simple/common explanation since it happened so consistently and immediately.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by prg »

Not putting things that need to survive a save/load into global would be a likely reason. Or you might be printing some debug information that contains table addresses which are allocated differently on every system. First things that come to mind...
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

Code: Select all

function init_powered_walls()
	script.on_event(defines.events.on_built_entity, build_powered_wall)
	script.on_event(defines.events.on_robot_built_entity, build_powered_wall)
	script.on_event(defines.events.on_preplayer_mined_item, mined_powered_wall)
	script.on_event(defines.events.on_robot_pre_mined, mined_powered_wall)
	script.on_event(defines.events.on_entity_died, death_powered_wall)
end

...

function build_powered_wall(event)
	local entity = event.created_entity
	if entity and entity.name == "powered-wall-wall" then
		local l_name = "powered-wall-pole"
		local l_surface = game.get_surface(game.players[event.player_index].surface.name)
		local l_force = entity.force
		local l_position = entity.position
		local l_wall = entity
		local l_pole = l_surface.create_entity{name = l_name, position = l_position, force = l_force}

		group_entities(cantor(l_position.x,l_position.y), { l_wall, l_pole })
	end
end
Here is a snippet of the relevant portion; this causes a guaranteed desync on placement every time in multiplayer, but still works and creates both entities; no issue manifests in single-player.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

I also, tried changing it to a queued a-sync event that triggers on tick update, but that still desyncs.
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by prg »

What's group_entities and cantor?

(A problem for later: in on_robot_built_entity, event won't contain a player_index)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

prg wrote:What's group_entities and cantor?

(A problem for later: in on_robot_built_entity, event won't contain a player_index)
Yes; it is a work in progress.

Code: Select all

function group_entities(entity_groupid, entity_list)
	return group("entities", entity_groupid, entity_list)
end
...

Code: Select all

-- Grouping
--
function group(index_id, group_id, members)
	_init_group(index_id, group_id)

	if index_id == nil then
	 index_id = "default"
 	end

	if group_id then
		for ix, vx in ipairs(members) do
			_addto_group(index_id, group_id, vx)
		end
		return group_id
	else
		-- no GID, then assign one
		return group(index_id, _new_group(index_id), members)
	end
end
...

and "cantor"; Standard Cantor-type hash

Code: Select all

-------------------------------------------------------------------
-- Cantor Hash
-------------------------------------------------------------------
function cantor(k1, k2)
	return (0.5 * (k1+k2) * (k1+k2+1) + k2)
end
-------------------------------------------------------------------
Rseding91
Factorio Staff
Factorio Staff
Posts: 15918
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by Rseding91 »

AenAllAin wrote:

Code: Select all

function init_powered_walls()
	script.on_event(defines.events.on_built_entity, build_powered_wall)
	script.on_event(defines.events.on_robot_built_entity, build_powered_wall)
	script.on_event(defines.events.on_preplayer_mined_item, mined_powered_wall)
	script.on_event(defines.events.on_robot_pre_mined, mined_powered_wall)
	script.on_event(defines.events.on_entity_died, death_powered_wall)
end

...

function build_powered_wall(event)
	local entity = event.created_entity
	if entity and entity.name == "powered-wall-wall" then
		local l_name = "powered-wall-pole"
		local l_surface = game.get_surface(game.players[event.player_index].surface.name)
		local l_force = entity.force
		local l_position = entity.position
		local l_wall = entity
		local l_pole = l_surface.create_entity{name = l_name, position = l_position, force = l_force}

		group_entities(cantor(l_position.x,l_position.y), { l_wall, l_pole })
	end
end
Here is a snippet of the relevant portion; this causes a guaranteed desync on placement every time in multiplayer, but still works and creates both entities; no issue manifests in single-player.
You've excluded a vast amount of the code which almost certainly contains the trouble code.
If you want to get ahold of me I'm almost always on Discord.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

Rseding91 wrote:
AenAllAin wrote:...
You've excluded a vast amount of the code which almost certainly contains the trouble code.
Yes; I didn't want to just blast a bunch of text at first; I'll post the whole mod in .zip form after I get home.

I was hoping it was something simple and a standard noobie mistake, not something that needed your complete parse.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: Multiplayer Desync on Item Creation

Post by AenAllAin »

Here is the mod/code. The desync issue is now gone it seems ...don't know why exactly. I updated the version number and the recipe name, and it stopped desyncing. I think it had something to do with the recipe and the saved game I was using and nothing to do with the code. I might be able to replicate it with a previous version and the saved game if there is any interest; I dunno.
Attachments
n^3_walls_0.0.7.zip
(307.09 KiB) Downloaded 77 times
Post Reply

Return to “Modding help”