How can I find the loading player's index?

Place to get help with not working mods / modding interface.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

How can I find the loading player's index?

Post by quyxkh »

I have a scenario in which I want to run some per-player remote calls on load (to change some default event responses when the scenario's running), but to provide backup implementations in case the mod's not present:

Code: Select all

on('load',function()
    if remote.interfaces.BenchmarkProduction.insta then
        remote.call('BenchmarkProduction','insta',  /* need to pass the player index here */)
    else
        do the bare-scenario stuff here
        end
    end)
I know why not to change game state in on_load, I use an on_tick dance not shown here for clarity. The problem I'm facing is, I want to issue that remote call for the player loading the scenario map, but I don't see how to find the player index of the player loading the map.

For multiplayer I could do it in on_player_joined_game, but trying it says that doesn't fire in singleplayer (side note, if it did, I wouldn't need the on_tick dance, ooh ooh and if on_player_left_game fired at the end that'd help with benchmarks too!).

Maybe some concrete details would help understand, perhaps there's an entirely different way to do what I want I haven't figured out?

The mod hooks the decon planner to provide an `iod` command allowing you to run arbitrary commands on selected entities, e.g. `/iod e.direction=2` and wave an inserter-selecting decon planner, all the selected inserters now point left. I'm adding an optional/per-player cheat facility like creative mode's, that instabuilds and instakills.

The scenario works fine without the mod, but I want it to provide the instakill/instabuild there too, and I want it to be the default. The way I'm going about it is to have the scenario control enable it remotely if the mod's there, or supply the hooks itself if it's not. If I could get the player index of the player loading the map, it'd be easy. I suspect I could do it by setting things up in on_init and tracking changes via a global updated by `on_configuration_changed`, but eww.

It looks like having on_player_{joined,left}_game fire even in single player would solve this, could I get that if there's not a better way to do what I want already?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14864
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How can I find the loading player's index?

Post by Rseding91 »

quyxkh wrote:I have a scenario in which I want to run some per-player remote calls on load (to change some default event responses when the scenario's running), but to provide backup implementations in case the mod's not present:
That isn't multiplayer safe and if you do it in multipalyer it will reject incoming connections stating your mod is broken (because it is).

You're not allowed to change *anything* in on-load because saving/loading the game is not meant to change anything in the game state or multiplayer doesn't work.
If you want to get ahold of me I'm almost always on Discord.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: How can I find the loading player's index?

Post by quyxkh »

The question remains, how do I get the player_index of the player loading the game from the on_load function to whatever does the actual call?

For the point you raised, though, do I understand you correctly, that even setting an on_tick event on load won't work because only that one client will execute the on_tick it schedules, and hence the only right way to get an on_tick event scheduled at all is to either have one always running that all clients always run or to do the work in on_player_joined? If so, that makes me really really want on_player_joined and on_player_left to run just after loading and just before shutdown in singleplayer as well, I'd appreciate any pointers to an explanation of a better way to do this.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14864
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How can I find the loading player's index?

Post by Rseding91 »

quyxkh wrote:The question remains, how do I get the player_index of the player loading the game from the on_load function to whatever does the actual call?
You can't and will never be able to.
quyxkh wrote:For the point you raised, though, do I understand you correctly, that even setting an on_tick event on load won't work because only that one client will execute the on_tick it schedules, and hence the only right way to get an on_tick event scheduled at all is to either have one always running that all clients always run or to do the work in on_player_joined?
Yes.
If so, that makes me really really want on_player_joined and on_player_left to run just after loading and just before shutdown in singleplayer as well, I'd appreciate any pointers to an explanation of a better way to do this.
In single player you don't join/leave the game. It just stops running. The only reason on_load exists is because event handlers, meta tables and locals aren't saved/loaded. If they where the event wouldn't exist.
If you want to get ahold of me I'm almost always on Discord.
Post Reply

Return to “Modding help”