validate my approach on scheduling an alert?

Place to get help with not working mods / modding interface.
Post Reply
hoylemd
Inserter
Inserter
Posts: 27
Joined: Tue Apr 17, 2018 2:14 am
Contact:

validate my approach on scheduling an alert?

Post by hoylemd »

Hey folks,

I'm still working on my autosave warning mod, and I've figure out a way to trigger the warnings, but my programmer-sense makes me think there might be a better way.

First, defining the problem:

I want to trigger an alert at some time before the autosave interval (assume the autosave interval is already available). For the sake of discussion, let's say this time before autosave (the 'offset') is 1 minute, or 3600 ticks. Assume the autosave interval (the 'interval') is set to 5 minutes (18000 ticks). I'll refer to those as 'offset' and 'interval' to keep it general, but those specific values are helpful for thinking about it.

My approach so far is this:
- register an `on_nth_tick` handler to fire every one minute(3600 ticks).
- in that handler, modulo the current game tick by the 'interval' to get the 'progress'. (so if it's at 2 minutes past the last autosave, the 'progress' is 7200 ticks)
- if interval - progress <= offset, fire the warning

This feels little clunky to me, since that handler needs to fire every `offset` ticks. That seems fine if offset is one minute, but if that offset is less (e.g. 30 seconds, or even 10 seconds), it's going to fire a *lot* without doing anything useful, and that seems wasteful.

If there's a better way to schedule something to happen after a certain number of ticks I think I'd prefer to have each alert be 'scheduled' by the previous one's handler (so when the handler fires, it sends the alert, then schedules the next one after `interval`)

Thanks folks!

SoShootMe
Filter Inserter
Filter Inserter
Posts: 477
Joined: Mon Aug 03, 2020 4:16 pm
Contact:

Re: validate my approach on scheduling an alert?

Post by SoShootMe »

hoylemd wrote:
Sun Apr 23, 2023 2:46 pm
This feels little clunky to me, since that handler needs to fire every `offset` ticks. That seems fine if offset is one minute, but if that offset is less (e.g. 30 seconds, or even 10 seconds), it's going to fire a *lot* without doing anything useful, and that seems wasteful.
Even checking (say) every second is not going to have a significant performance impact, so almost certainly you shouldn't worry. However the approach you describe does rely on the offset being a divisor of the interval for the alert to always be at the intended time.
If there's a better way to schedule something to happen after a certain number of ticks I think I'd prefer to have each alert be 'scheduled' by the previous one's handler (so when the handler fires, it sends the alert, then schedules the next one after `interval`)
Because on_nth_tick runs the handler when the tick is a multiple (including zero) of the tick argument, you can use it to schedule a handler to run at a particular future tick by specifying the desired tick as the tick argument, computed in your case from the current tick, offset and interval. In the handler, you need to deregister the existing handler (script.on_nth_tick(event.tick, nil)) then register a new one for the next time.

hoylemd
Inserter
Inserter
Posts: 27
Joined: Tue Apr 17, 2018 2:14 am
Contact:

Re: validate my approach on scheduling an alert?

Post by hoylemd »

Great! Thanks for the help. I do tend to overestimate how bad performance impacts are, so I'm glad to have that reality check!

I did implement this , and it did seem to work without the offset being a divisor of the interval though (e.g. autosave interval at 5 minutes, warnings at 2 and 1 minute offsets worked as intended). I think you're on the right track for if the checker's interval isn't a common factor of the autosave interval and offset. e.g. if I want a warning at 30s before the autosave, that one would never actually trigger because the checker would only fire at 1m before autosave and at the autosave itself. But I could use the approach you suggested to schedule alerts between minutes if I end up doing that, so thanks!

edit: And version 0.1.0 is published! https://mods.factorio.com/mod/autosave-alert

Post Reply

Return to “Modding help”