Auto Manual Mode

by Roy192

When in a train, using the movement controls will automatically set the train in manual mode.

Utilities
3 years ago
0.16 - 1.1
8.80K

g The mod doesn't work.

2 years ago

Hello, having installed your mod, and going into the game.
Got on a train with an automatic mode of travel between stations.
But the train does not switch to manual mode.

How to fix it?

2 years ago

Did you press any of the WASD keys? The train is supposed to go into manual mode if you try to drive the train yourself.

2 years ago

Yes.
That's what I figured.
But the mod just doesn't work...
I press the WASD control keys - the default in the game.
But the switch to manual mode is not.
Maybe it's the version of the game?
I installed the mod on version 1.1.50
It used to work.
I've used it before, but on version 1.1.44

I don't know what the problem is.

2 years ago

I can confirm that the mod does not work. Just discovered this mod and installed it for 1.1.50. Embarked a train on an automatic schedule that was parked at station and wanted to leave by pressing W or S but nothing happens. The train does not switch to manual.

2 years ago

Please fix the mod, thank you so much!

2 years ago

Update the mod, please.

1 year, 9 months ago

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

1 year, 9 months ago
(updated 1 year, 9 months ago)

@azaghal
I (and many other, too, I guess) would HIGHLY appreciate a patch for this to work. 8)

1 year, 9 months ago

Hm... Let me see if there's any kind of reply in the next couple of days before I go ahead and start forking yet another mod... Seems like I've done quite a bit of that in last couple of weeks :)

But don't worry - the patch or fork will come, I kinda want to have a feature like this, with maybe an addition of easily opening the train window (for being able to pick manual destination point).

1 year, 9 months ago

This would be great. The features alone makes that a no-brainer to use.

1 year, 9 months ago

Anything new about the patch ... ?

1 year, 9 months ago

Unfortunately not yet, I was a bit busy with another mode that took quite a bit of my time. Let me see if I can get some kind of quick drop-in patch going here, although it will not fix all the issues with this mod.

1 year, 9 months ago

We will take what we can get. :)
Thanks in advance anyway.

1 year, 9 months ago

Here is a full replacement for the control.lua script:

script.on_init(
    function()
        global.auto_manual_mode = global.auto_manual_mode or {}
    end
)

script.on_load(
    function()
        global.auto_manual_mode = global.auto_manual_mode or {}
    end
)

script.on_event({"up-event", "down-event", "left-event", "right-event"},
    function(e)

        local vehicle = game.players[e.player_index].vehicle;

        if vehicle and vehicle.train and game.players[e.player_index].render_mode == defines.render_mode.game then
            if not vehicle.train.manual_mode then
                global.auto_manual_mode[e.player_index] = vehicle.train.id;
            end

            vehicle.train.manual_mode = true;
        end
    end
);

script.on_event(
    defines.events.on_player_driving_changed_state,
    function(e)

        local player = game.players[e.player_index];
        local vehicle = e.entity;

        if player and vehicle and vehicle.train and settings.get_player_settings(player)["auto_manual_mode_restore_when_exit"].value and vehicle.train.id == global.auto_manual_mode[e.player_index] then
            vehicle.train.manual_mode = false;
        end

        global.auto_manual_mode[e.player_index] = 0;
    end
);

If you try it out and it crashes or something, do let me know. I think it should also solve the desync issues reported by people, but I haven't tested it.

1 year, 8 months ago

Kudos to you!
All is working perfectly as far as I have tested it. This is a great mod! Many thanks for sharing this correction! :)

10 months ago

Is there a fork anywhere? I’d love a fork upload.

8 months ago

@azaghal tried to use your version of control and game throwed at me that changing global in on_load is prohibited ...
any chance you could create updated version?

8 months ago

Hm... Don't have time to get into the details, but... If you drop the script.on_load() function, it should work out fine for you - particularly if you are adding this mod to a new savegame. At least if that's the only problem with the code :)

New response