Hello folks (and Roy192 in particular :) ),
I ran into this mod, thought it to be a very nifty idea, and saw this thread (alongside the one about desync). So, I decided to dig into the code a little bit :)
The main reason why the mod does not work is because the script.on_load
handler only runs when the game is loaded and the mod has already been part of the save previously. It also gets run for players connecting to a running multiplayer session.
The handler takes care of setting-up the global structure, and without that global structure being set-up, code execution will fail at this line (since auto_manual_mode
is nil
at this point) :
auto_manual_mode[e.player_index] = vehicle.train.id;
However, the use of pcall
effectively masks the error.
So, interesting enough, if you start a game with the mod present, save, then reload, the structure will get reinitialised, and at least in single player the mod will work. For more info on on_init()
/on_load()
/on_configuration_changed()
invocation and ordering, see Data lifecycle/Save startup.
@Roy192 To fix this, I would suggest the following (if you are interested, I would gladly submit a patch):
- Drop the use of
pcall
- the way it is used currently, it is more likely to mask the problems than help. If game crashes, let it crash, and fix the bug instead. This is particularly true since the mod is fairly simple. There's probably some good uses for pcall
, but they're probably quite esoteric.
- Initialise the global structure via
script.on_init()
handler - this should cover new games.
- Initialise the global structure via
script.on_configuration_changed()
handler - this should cover people upgrading to new version of this mod.
- Instead of referencing the
auto_manual_mode
via local, access it through global.auto_manual_mode
- this is more a suggestion to make it clearer where the refrenced variable value really comes from.
Best regards,
Branko