[Solved] Conditionally registering for custom event

Place to get help with not working mods / modding interface.
eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

[Solved] Conditionally registering for custom event

Post 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?
Last edited by eduran on Mon Apr 15, 2019 2:48 pm, edited 1 time in total.
Bilka
Factorio Staff
Factorio Staff
Posts: 3684
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Conditionally registering for custom event

Post 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
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: [Solved] Conditionally registering for custom event

Post by eduran »

Thank you, that is exactly what I was looking for.
Post Reply

Return to “Modding help”