on_chunk_generated causing mp desync
Posted: Wed Jan 23, 2019 3:12 am
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 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
my best guess at this point is that first line outside the script
Code: Select all
local firstrock = trueam 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)