Page 1 of 1

[Solved] Conditionally registering for custom event

Posted: Mon Apr 15, 2019 2:01 pm
by eduran
I ran into an issue with a conditional event that I don't know how to solve. Here is the situation:
  • Mod A has an optional dependence on Mod B
  • If Mod B exists, Mod A uses the remote interface to get a custom event ID from Mod A in on_init and on_configuration_changed:

    Code: Select all

    -- part of on_init and on_configuration_changed of Mod A
    if game.active_mods["Mod_B"] then
      global.custom_event = remote.call("Mod_B_interface", "get_event")
      script.on_event(global.custom_event, custom_event_handler)
    else
      global.custom_event = nil
    end
    
  • This is a conditional event and has to be re-registered in on_load:

    Code: Select all

    -- part of on_load of Mod A
    if global.custom_event then
      script.on_event(global.custom_event, custom_event_handler)
    end
    
Now the problem: On_load runs before on_configuration_changed. If I save the game with Mod A and B both active, disable Mod B and reload the save, global.custom_event is no longer valid. This results in an "Given event (xxx) is not a valid event." error. I can't check game.active_mods in on_load and there is no way to check if an event ID is valid.
Any suggestions on how to solve this in a desync-free way?

Re: Conditionally registering for custom event

Posted: Mon Apr 15, 2019 2:35 pm
by Bilka
Dont use game.active_mods(). Use remote.interfaces to check for the existence of the interface (and the function) and dont put it into global at all. You can do that in on load too, so you have the same code in both events.

Example of checking for existence of the remote interface: https://wiki.factorio.com/Tutorial:Scri ... interfaces

Re: [Solved] Conditionally registering for custom event

Posted: Mon Apr 15, 2019 2:49 pm
by eduran
Thank you, that is exactly what I was looking for.