[Lua] Are timers the most optimal solution for checking values periodically?

I want to code two new components for my mod which would be used by many objects so it is important they do not cause much lag when they are not needed.

  • Accumulator component requires a counter which calculates the amount of points added to a counter in short intervals (like an in-game hour) based on values taken from a custom server service (asks for values and performs very simple arithmetic, nothing more), then checks if a certain value was reached and takes action if it was.
  • Sentinel component which checks if a value in a custom server service has changed and perform an action if it did. The thing is this value will change very rarely, few times per in-game year and always at midnight, so one check per in-game day would be enough.

As far as I know the least intensive solution would be applying timers. Is there any more lightweight solution?

Not sure about the first but this one sounds like you might be able to use a listener instead?

Yes but is a listener, which also consumes memory constantly, better than a timer?

Not sure about lua but in general, as far as I know, a listener is not much more than a pointer, so it uses very little memory. Compared to a timer, which I assume updates every tick and checks if it’s time yet, I would definitely go for the listener.

There might be another alternative that could be useful to you. I think I’ve seen some schedule function when looking through code. Basically “run this function at this time”. I assume it is then placed as a task in a queue connected to a shared timer. I think I saw it while looking through the code for ladders but a more obvious place too look would probably be the event handler (enemy attacks and that sort of events).

1 Like

Oh yeah, I remember it too. The name was related to an alarm clock, it was in the calendar service. I’ll check it, thanks!

1 Like