Factorio events not always fired the same way?

Place to get help with not working mods / modding interface.
credomane
Filter Inserter
Filter Inserter
Posts: 321
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Factorio events not always fired the same way?

Post by credomane »

How exactly are events fired in Factorio? I've been working on a mod for a while and the experience with my test mod is that I'm amazed that any mod works at all in factorio. :(

My mod listens for these events: on_init, on_load, on_tick, on_new_player.

New Game -> Everything is fine. on_init, on_load, on_tick, on_new_player are fired and the game variable exists and my mod's button that is created in the on_new_player event appears in the top bar just fine.
Load saved game previously without mod installed. on_init, on_load, on_tick are fired but on_new_player is not. This isn't a big deal and explains why all mods duplicate gui creation code between on_load and on_new_player. Easily fix in my mod.
Load save game previously with mod installed. on_init, on_load, on_tick are fired but on_new_player is not. Same reason as before. Now for the frustrating part. the "game" variable is suddenly nil for on_init and on_load but on_tick is fine.

Code: Select all

Unknown key:"Error while running the on_load: __CTest__/Ctest.lua:15: attempt to index global 'game' (a nil value)"
OK. So I check if game is nil first. Well now when loading a game that was saved with my mod installed there are no errors but no mod button and no "[debug] whatever" messages. My mod might as well not exist.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Factorio events not always fired the same way?

Post by DaveMcW »

For button creation, on_init and on_player_created are enough. You don't need to mess with on_load and its hatred of "game".
Rseding91
Factorio Staff
Factorio Staff
Posts: 15950
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Factorio events not always fired the same way?

Post by Rseding91 »

on_init is only called when making a brand new world.

on_load is only called when loading a world and is designed to not have access to "game" as you should never change the game state in the load event.

on_tick can do what ever you like and always has access to "game".

You don't seem to understand how the basics of the event system are laid out yet.
If you want to get ahold of me I'm almost always on Discord.
credomane
Filter Inserter
Filter Inserter
Posts: 321
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Re: Factorio events not always fired the same way?

Post by credomane »

Rseding91 wrote:You don't seem to understand how the basics of the event system are laid out yet.
Agreed. Definitely not. I've made mods, extensions, and plugins for plenty of games and programs over the years. Be it c, php, Java, lua or HTML/JavaScript. Especially for bukkit and Minecraft. I don't know if the determinism design of Factorio is throwing me off or what but the way on_init and on_load work are odd to me. I found this guide on the Factorio wiki that helped a lot. https://wiki.factorio.com/index.php?tit ... _Lifecycle

This showed me I have been operating on an incorrect assumption because of my past experience with event systems and the documentation on the Factorio api site. Particularly this one. http://lua-api.factorio.com/0.12.35/LuaBootstrap.html The end result gave me a completely wrong impression of how those two events function. I now realize that for my mod on_load is useless and I need to move what I had in there to on_init plus some other changes.

I appreciate all the help and sorry for being rude. What I thought was a straightforward and simple mod for my testing the waters Factorio mod is proving to be quite the opposite. Starting to get more than a bit frustrated as a result.

Once I have power again I can rework my mod. Hopefully all goes well and I can post my mod the the forums sometime next week. :D
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Factorio events not always fired the same way?

Post by DaveMcW »

Rseding91 wrote:on_init is only called when making a brand new world.
on_init is also called when adding a brand new mod to an existing world.
Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: Factorio events not always fired the same way?

Post by Supercheese »

DaveMcW wrote:on_init is also called when adding a brand new mod to an existing world.
Noooo, that would be on_configuration_changed.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Factorio events not always fired the same way?

Post by DaveMcW »

It would ALSO be on_init.

Proof:
on-init-alert_0.0.1.zip
(550 Bytes) Downloaded 65 times
credomane
Filter Inserter
Filter Inserter
Posts: 321
Joined: Tue Apr 12, 2016 6:21 pm
Contact:

Re: Factorio events not always fired the same way?

Post by credomane »

Supercheese wrote:
DaveMcW wrote:on_init is also called when adding a brand new mod to an existing world.
Noooo, that would be on_configuration_changed.
on_init is called when adding a mod to an existing world.
on_configuration_change event will also fire after on_init and on_load events have completed.
Post Reply

Return to “Modding help”