Force event and lookup consistency

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
User avatar
raiguard
Factorio Staff
Factorio Staff
Posts: 451
Joined: Wed Dec 13, 2017 8:29 pm
Contact:

Force event and lookup consistency

Post by raiguard »

Hello. I have noticed that there appear to be some inconsistencies between on_force_created and on_surface_created. The game only appears to fire on_force_created when a force is created through LUA, and so doesn't get fired when a new map is created with the three default forces. On the other hand, on_surface_created fires both when nauvis is created, and when a new surface is created through LUA. I would like to request that on_force_created also fire when the game itself makes a force, not just when one is created through LUA.

Additionally, in game, you can get players and surfaces by index number, but you can only get a force by name. I would like to request that game.forces should be able to accept a force's index number as an alternative to the force's name.

Thanks for your time!
Don't forget, you're here forever.

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Force event and lookup consistency

Post by Boodals »

Raiguard wrote:
Sun Jul 21, 2019 9:17 pm
Hello. I have noticed that there appear to be some inconsistencies between on_force_created and on_surface_created. The game only appears to fire on_force_created when a force is created through LUA, and so doesn't get fired when a new map is created with the three default forces. On the other hand, on_surface_created fires both when nauvis is created, and when a new surface is created through LUA. I would like to request that on_force_created also fire when the game itself makes a force, not just when one is created through LUA.
I believe the correct behavior is that on_surface_created shouldn't fire when starting a new game. If you need to run some logic on every surface/force/player/whatever, you are supposed to loop through the existing ones in on_init, then listen to new ones with the various events. This makes a lot more sense when you consider that the mod may be added part way through an existing game.
Raiguard wrote:
Sun Jul 21, 2019 9:17 pm
Additionally, in game, you can get players and surfaces by index number, but you can only get a force by name. I would like to request that game.forces should be able to accept a force's index number as an alternative to the force's name.
I just checked the source code, this seems to be already implemented, just not documented.

Bilka
Factorio Staff
Factorio Staff
Posts: 3133
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Force event and lookup consistency

Post by Bilka »

Boodals wrote:
Mon Jul 22, 2019 4:36 am
Raiguard wrote:
Sun Jul 21, 2019 9:17 pm
Additionally, in game, you can get players and surfaces by index number, but you can only get a force by name. I would like to request that game.forces should be able to accept a force's index number as an alternative to the force's name.
I just checked the source code, this seems to be already implemented, just not documented.
Indeed, this was added recently. I fixed the doc for the next version. If you find an event that doesn't provide a force index (only a force name) please let us know.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: Force event and lookup consistency

Post by Rseding91 »

Unless I'm missing something on_surface_created does not fire when the default surface is created.
If you want to get ahold of me I'm almost always on Discord.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Force event and lookup consistency

Post by orzelek »

Rseding91 wrote:
Tue Jul 23, 2019 11:13 am
Unless I'm missing something on_surface_created does not fire when the default surface is created.
It doesn't and it's a sad discovery. It also doesn't fire in few other scenarios I think (like creation of surface in on_init of some mod.. maybe if it runs before your mod).
It basically means that this event is only marginally useful.

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Force event and lookup consistency

Post by Boodals »

orzelek wrote:
Tue Jul 23, 2019 5:49 pm
(like creation of surface in on_init of some mod.. maybe if it runs before your mod).
As explained in the Data Lifecycle page of the API, under "4. control.lua init":
Note: until on_init has finished executing for a given mod (scenario scripts are also counted as a mod) the game will not raise any game events for that mod.
If you want to fire events for your own mod to pick up, you need to manually call the function you have registered to the event within on_init.
This was done so that you don't get events called before your global tables have been created in on_init. I'm not sure why events aren't raised from within your own on_init though. Personally i'd prefer if it would fire your events from inside your on_init. I've ran into several bugs that weren't easy to track down because of this behavior. Now i've written a wrapper for raise_event that checks if on_init has completed, and if not it will manually call all functions registered to the event manually.

User avatar
raiguard
Factorio Staff
Factorio Staff
Posts: 451
Joined: Wed Dec 13, 2017 8:29 pm
Contact:

Re: Force event and lookup consistency

Post by raiguard »

Yes, turns out that I was wrong about the event firing when nauvis was created. I guess it's good to keep everything as-is. Thanks guys!
Don't forget, you're here forever.

Post Reply

Return to “Modding interface requests”