on_chunk_generated causing mp desync

Place to get help with not working mods / modding interface.
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

on_chunk_generated causing mp desync

Post by kingarthur »

i think i did a dumb. i got a post today on my pyblock mod that in my control.lua file on_chunk_generated is causing the game to desync after 10-30 secounds in multiplayer. it does not cause any issues in single player. now my first assumption was math.random is of course the issue but according to several posts here on the forums math.random shouldnt be the issue.

my best guess at this point is that first line outside the script

Code: Select all

local firstrock = true
is what is cuasing the issue as if i understand what ive done correctly it starts as true for all players as the join and then at some point gets set to false for one or more players while another player has it still true. so to fix i need to get rid of that firstrock variable and find a different way of checking that has to be the same for every player no matter what.

am i on the right track with this or is it something else ive done

Code: Select all

local firstrock = true
				
script.on_event(defines.events.on_chunk_generated, function(event)

local SelectedRock = math.random(1,15)

--log(serpent.block(event))

local tx = event.area.left_top.x
local ty = event.area.left_top.y
local bx = event.area.right_bottom.x
local by = event.area.right_bottom.y

local Randx = math.random(tx,bx)
local Randy = math.random(ty,by)

local tiles = {}

local x = Randx - 5
local y = Randy - 5

local a=0
local b=0

local RandChance

if firstrock == true then

	SelectedRock = 1
	
	RandChance = math.random(0,40)
	
else

	RandChance = math.random(0,480)
	
end

if RandChance == 5 then

for i = 0,121 do

	table.insert(tiles,{name="grass-1", position={x,y}})
	
	x = x+1
	
	a=a+1
		
		if a==11 then
		
		x=Randx-5
		
		y=y+1
		
		b=b+1
		
		a=0
		
			if b==11 then
			
			y=Randy-5
			
			b=0
			
			end
		
		end
		
end

game.surfaces["nauvis"].set_tiles(tiles)

game.surfaces["nauvis"].create_entity{name=Rocks[SelectedRock],position={Randx,Randy},amount=math.random(1000000,15000000)}

if firstrock == true then

	firstrock = false
	
end

end

end)
Rseding91
Factorio Staff
Factorio Staff
Posts: 16013
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_chunk_generated causing mp desync

Post by Rseding91 »

All you have to do is store the variable in the "global" table. It will get saved/loaded and persist between sessions/when someone joins in multiplayer.
If you want to get ahold of me I'm almost always on Discord.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: on_chunk_generated causing mp desync

Post by darkfrei »

Yeah, if you define

Code: Select all

local firstrock = true
Then it must be not changed anymore.
Set up it in on_init:

Code: Select all

global = {}
global.firstrock = true
Rseding91
Factorio Staff
Factorio Staff
Posts: 16013
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_chunk_generated causing mp desync

Post by Rseding91 »

You don't need to set global = {}, it's an empty table by default.
If you want to get ahold of me I'm almost always on Discord.
kingarthur
Smart Inserter
Smart Inserter
Posts: 1463
Joined: Sun Jun 15, 2014 11:39 am
Contact:

Re: on_chunk_generated causing mp desync

Post by kingarthur »

Ok. I'll make the necessary changes. An older post of rseding clues me into where I screwed up but I didn't realize I could use the global table like that. My fault for not checking the wiki lately. Seems it's been updated since I last looked at it
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: on_chunk_generated causing mp desync

Post by darkfrei »

Rseding91 wrote: Wed Jan 23, 2019 10:44 am You don't need to set global = {}, it's an empty table by default.
I've seen that some modes use glob instead of global, wasn't sure that it exists before.
Post Reply

Return to “Modding help”