validate my approach on scheduling an alert?
Posted: Sun Apr 23, 2023 2:46 pm
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!
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!