on_surface_created event question

Place to get help with not working mods / modding interface.
Post Reply
orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

on_surface_created event question

Post by orzelek »

I've run into small inconsistency with this event.
Documentation states that it's not called for nauvis since that surface exists from the start.

I have encountered a case where it's not called when surface is created by game.create_surface call. It seems that event doesn't fire when Oarc mod is creating it's surface during on_init event.

Is that a planned behavior?

eduran
Filter Inserter
Filter Inserter
Posts: 344
Joined: Fri May 09, 2014 2:52 pm
Contact:

Re: on_surface_created event question

Post by eduran »

Most events only trigger when the base game does something and not off of mod scripts. If a mod creates a surface and want others to notice, it has to use script.raise_event. If the mods in questions don't do that you are not going to see the event. So yes, that is working as intended.

Don't mind me, read below (in case anyone comes across this later).
Last edited by eduran on Wed May 08, 2019 7:16 pm, edited 1 time in total.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_surface_created event question

Post by eradicator »

eduran wrote:
Wed May 08, 2019 6:14 pm
Most events only trigger when the base game does something and not off of mod scripts. If a mod creates a surface and want others to notice, it has to use script.raise_event. If the mods in questions don't do that you are not going to see the event. So yes, that is working as intended.
Have a counterexample:

Code: Select all

/c script.on_event(defines.events.on_surface_created,function(e) game.print('created!') end) game.create_surface('test')
The base game never creates new surfaces, and there are no other events that are only used with script.raise_event, except for the three explicitly named ones.

@orzelek:
Is *your* mod before or after that other mod in the load oder? /c game.print(script.get_event_order())
If it is *after* the other mod then *you* don't get the event, because a while ago (like a year ago :p) there
was a change that made it so that no mod can recieve *any events* before it's own on_init has been called.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: on_surface_created event question

Post by orzelek »

eradicator wrote:
Wed May 08, 2019 6:23 pm
eduran wrote:
Wed May 08, 2019 6:14 pm
Most events only trigger when the base game does something and not off of mod scripts. If a mod creates a surface and want others to notice, it has to use script.raise_event. If the mods in questions don't do that you are not going to see the event. So yes, that is working as intended.
Have a counterexample:

Code: Select all

/c script.on_event(defines.events.on_surface_created,function(e) game.print('created!') end) game.create_surface('test')
The base game never creates new surfaces, and there are no other events that are only used with script.raise_event, except for the three explicitly named ones.

@orzelek:
Is *your* mod before or after that other mod in the load oder? /c game.print(script.get_event_order())
If it is *after* the other mod then *you* don't get the event, because a while ago (like a year ago :p) there
was a change that made it so that no mod can recieve *any events* before it's own on_init has been called.
My mod would be after - no dependencies present and alphabetically it's oarc-mod vs rso-mod.
That could explain why I don't get the event at all.. and it just means even more if's in strange places in my code.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_surface_created event question

Post by eradicator »

orzelek wrote:
Wed May 08, 2019 6:52 pm
That could explain why I don't get the event at all.. and it just means even more if's in strange places in my code.
You can simulate the event by calling your own handler in on_init.

Code: Select all

script.on_init(function()
  --stuff
  --more stuff
 
  for _,surface in pairs(game.surfaces) do
    rso_on_surface_created_handler_thingy{
      name = defines.events.on_surface_created,
      tick = game.tick,
      surface_index = surface.index
      }
    end
    
  end)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: on_surface_created event question

Post by orzelek »

I've ended up just checking it anyway in few more places... which leads me to conclusion that in current state this event is less then useful.
Since there is no guarantee that it will be called for each new surface then we can't rely on it to initialize any per surface data structures. This means that any method that expects this data to exist needs to still include a check for it's presence. So we might as well skip the event completely and rely on ongoing checks.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_surface_created event question

Post by eradicator »

orzelek wrote:
Tue May 14, 2019 9:36 pm
[...] which leads me to conclusion that in current state this event is less then useful.[...]
So we might as well skip the event completely and rely on ongoing checks.
From what i understand about the engine my method posted above (raising it for all surfaces in your own init) *does* guarantee it gets called for all surfaces.

And if it does not than that problem persists for almost any event (tech_researched, entity_built, etc...).
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: on_surface_created event question

Post by orzelek »

eradicator wrote:
Wed May 15, 2019 7:16 am
orzelek wrote:
Tue May 14, 2019 9:36 pm
[...] which leads me to conclusion that in current state this event is less then useful.[...]
So we might as well skip the event completely and rely on ongoing checks.
From what i understand about the engine my method posted above (raising it for all surfaces in your own init) *does* guarantee it gets called for all surfaces.

And if it does not than that problem persists for almost any event (tech_researched, entity_built, etc...).
I've ended up doing that but in mods changed event and in on init. I'm not sure what happens if you add a mod to game that will create surface in it's own on_init. And doing that on mods update was needed for other reasons too - like potential resource list changes.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_surface_created event question

Post by eradicator »

orzelek wrote:
Wed May 15, 2019 7:50 am
I'm not sure what happens if you add a mod to game that will create surface in it's own on_init.
If your mod was already in the savegame then your mods on_init() has already been run and you should recieve the event just fine. (As the non-recieving is entirely based on your own on_init() *not* having been called yet.)
But ofc it won't hurt to do it in on_config() too. I just didn't wanna see you drop into the hell where you can't rely on any events at all anymore ;p.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”