Page 1 of 1

conditional events by global settings cause MP desync

Posted: Fri May 19, 2017 7:42 pm
by Optera
I have problems figuring out why this desync.

Code: Select all

local use_train = settings.global["loader-use-trains"].value

script.on_load(function()
  if global.loaders and next(global.loaders) then
    script.on_event(defines.events.on_tick, ticker)
  end
  if use_train == "disabled" then
    script.on_event(defines.events.on_train_changed_state, nil)
    script.on_event(defines.events.on_train_created, nil)
  else
    script.on_event(defines.events.on_train_changed_state, train_update)
    script.on_event(defines.events.on_train_created, train_update)
  end
end)
Checksum for script LoaderRedux/control.lua: 1212318411
233.645 Error ClientMultiplayerManager.cpp:950: mod-LoaderRedux was not registered for the following events when the map was saved but has registered them as a result of loading: on_train_changed_state (ID 23) and on_train_created (ID 66)
233.645 Error ClientMultiplayerManager.cpp:90: MultiplayerManager failed: "" + multiplayer.script-event-mismatch + "
" + "
mod-LoaderRedux"
233.646 Info ClientMultiplayerManager.cpp:539: MapTick(881) changing state from(ConnectedLoadingMap) to(Failed)
Shouldn't global settings be synchronized before running on_load?

Re: conditional events by global settings cause MP desync

Posted: Sat May 20, 2017 2:43 am
by Nexela
You forgot to check for and conditionally register the train event in on_int :) on_load does not run for mods that have not had on_init ran. So on_init was NOT registering on_train and on_load was resulting in mismatched registration.

Re: conditional events by global settings cause MP desync

Posted: Sat May 20, 2017 5:40 am
by Optera
Thanks. I keep forgetting on_load doesn't fire every load.

Re: conditional events by global settings cause MP desync

Posted: Sun May 21, 2017 4:59 am
by Rseding91
Optera wrote:Thanks. I keep forgetting on_load doesn't fire every load.
Well, technically it does. Just when you create a map you aren't loading it but creating it :P

Re: conditional events by global settings cause MP desync

Posted: Sun May 21, 2017 9:46 am
by DaveMcW
Rseding91 wrote:when you create a map you aren't loading it but creating it :P
Why not call on_load anyway?

The Android API calls onResume() the first time an Activity is created, this makes it very convenient to put all your setup stuff in one place.

Re: conditional events by global settings cause MP desync

Posted: Sun May 21, 2017 7:21 pm
by Rseding91
DaveMcW wrote:
Rseding91 wrote:when you create a map you aren't loading it but creating it :P
Why not call on_load anyway?

The Android API calls onResume() the first time an Activity is created, this makes it very convenient to put all your setup stuff in one place.
Because on_init and on_load are 2 very different events with very different purposes.

Re: conditional events by global settings cause MP desync

Posted: Sun Nov 18, 2018 3:49 am
by Zaflis
Rseding91 wrote:..
Since this thread became relevant again, i have to clarify those for further confusion.
https://lua-api.factorio.com/latest/Lua ... ap.on_load
This is meant for 3 specific reasons and only 3:
- re-register conditional event handlers
- re-setup meta tables
- create local references to tables stored in the global table
VehicleSnap uses conditional event handlers now, so if player was in car and i loaded a game like that, snapping was not registering until i re-entered the car. That was until i registered the event in on_load. But that then caused desync on server when no players are in vehicles it unregisters them, and then player joining server causes his local instance to register events in on_load and desync. So i had to check if server had them on first

Code: Select all

script.on_load(function()
  if global.RegisterEvents then
    ToggleEvents(true)
  end
end)
Set

Code: Select all

global.RegisterEvents = enable
in ToggleEvents(enable) function. It sounds logical to me, correct? I'm just wondering why the game doesn't save the state of events automatically.

Re: conditional events by global settings cause MP desync

Posted: Mon Nov 19, 2018 1:42 am
by Rseding91
Zaflis wrote:
Sun Nov 18, 2018 3:49 am
Rseding91 wrote:..
Since this thread became relevant again, i have to clarify those for further confusion.
https://lua-api.factorio.com/latest/Lua ... ap.on_load
This is meant for 3 specific reasons and only 3:
- re-register conditional event handlers
- re-setup meta tables
- create local references to tables stored in the global table
VehicleSnap uses conditional event handlers now, so if player was in car and i loaded a game like that, snapping was not registering until i re-entered the car. That was until i registered the event in on_load. But that then caused desync on server when no players are in vehicles it unregisters them, and then player joining server causes his local instance to register events in on_load and desync. So i had to check if server had them on first

Code: Select all

script.on_load(function()
  if global.RegisterEvents then
    ToggleEvents(true)
  end
end)
Set

Code: Select all

global.RegisterEvents = enable
in ToggleEvents(enable) function. It sounds logical to me, correct? I'm just wondering why the game doesn't save the state of events automatically.
Yes that's correct. The game doesn't save events because it isn't possible to do unless we force that control.lua doesn't change for the lifetime of a save file.