Page 1 of 1

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

Posted: Wed Jul 29, 2015 3:49 am
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.

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

Posted: Wed Jul 29, 2015 6:27 am
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.

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

Posted: Wed Jul 29, 2015 6:45 pm
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.