Help me, in my mod“script.on_init”not run

Place to post guides, observations, things related to modding that are not mods themselves.
sdgmlj
Fast Inserter
Fast Inserter
Posts: 174
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Help me, in my mod“script.on_init”not run

Post by sdgmlj »

It only runs when a new game is created and does not run when reading saved files
To verify this issue, I deleted all the content in the mod, but kept the following content in "control. lua":
script.on_init(function()
for i, player in pairs(game.players) do
player.print({"message.message"})
end
end)
When reading the archive, it still does not run
User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 469
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Help me, in my mod“script.on_init”not run

Post by Stringweasel »

This is intended behaviour. If you look at the Life Cycle you can see on_init is only run when a new game is created.

When an existing save is loaded then on_load is called instead. However, please keep in mind that for 99.9% of cases mods don't have to do anything in this event, and it's also very easy to cause desyncs. For 99.9% of cases you'd want to use on_configuration_changed instead.
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
sdgmlj
Fast Inserter
Fast Inserter
Posts: 174
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help me, in my mod“script.on_init”not run

Post by sdgmlj »

Stringweasel wrote: Tue Oct 29, 2024 8:35 am This is intended behaviour. If you look at the Life Cycle you can see on_init is only run when a new game is created.

When an existing save is loaded then on_load is called instead. However, please keep in mind that for 99.9% of cases mods don't have to do anything in this event, and it's also very easy to cause desyncs. For 99.9% of cases you'd want to use on_configuration_changed instead.
Thanks
Post Reply

Return to “Modding discussion”