conditional events by global settings cause MP desync

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

conditional events by global settings cause MP desync

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

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: conditional events by global settings cause MP desync

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

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: conditional events by global settings cause MP desync

Post by Optera »

Thanks. I keep forgetting on_load doesn't fire every load.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: conditional events by global settings cause MP desync

Post 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
If you want to get ahold of me I'm almost always on Discord.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: conditional events by global settings cause MP desync

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: conditional events by global settings cause MP desync

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Zaflis
Filter Inserter
Filter Inserter
Posts: 417
Joined: Sun Apr 24, 2016 12:51 am
Contact:

Re: conditional events by global settings cause MP desync

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: conditional events by global settings cause MP desync

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Modding help”