[0.12.1] Mods' on_load event called at the wrong time

Bugs that are actually features.
Post Reply
johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

[0.12.1] Mods' on_load event called at the wrong time

Post by johanwanderer »

In version 0.12.1 (didn't test with 0.12.0), mods' on_load events are called when the user save a game, not when the user load it.

Please see the attached (test) mod
LongTestEvents_0.12.1.zip

Code: Select all

require "defines"

-----------------------------------------------------------
-- utility functions
-----------------------------------------------------------
function print(s)
  for index, player in pairs(game.players) do
    if(player.valid) then
      player.print(s);
    end;
  end
end

game.on_init(function(event)
  print("oninit()");
end);

game.on_load(function(event)
  print("onload()");
end);

game.on_event(defines.events.on_tick, function(event)
  print("initial tick");
  game.on_event(defines.events.on_tick, nil);
end);
You would expect to see oninit(), followed by onload(), then oninitialtick(). However, onload() won't be called until you actually save the game.

This represents a problem whenever mods want to initialize data on game loading. Workaround: perform on-load tasks during the initial tick.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.12.1] Mods' on_load event called at the wrong time

Post by Rseding91 »

That's expected behavior.

on_init is called on game creation.
on_save is called right before the game is saved.
on_load is called right after the game is saved and when the user loads the game.

The reason for this is so mods can setup data they need saved in the format they need it to be saved as then after the save is done on_load is called and they restore to the normal working state.
If you want to get ahold of me I'm almost always on Discord.

johanwanderer
Fast Inserter
Fast Inserter
Posts: 157
Joined: Fri Jun 26, 2015 11:13 pm

Re: [0.12.1] Mods' on_load event called at the wrong time

Post by johanwanderer »

Rseding91 wrote:That's expected behavior.
on_load is called right after the game is saved and when the user loads the game.
Except that on_load is currently NOT called when the user first load the game.

Post Reply

Return to “Not a bug”