0
\$\begingroup\$

I want to implement a logic that will execude after a while when no input was hapend. Like a screensaver that will get started after a few minutes of no mouse input.

Im using libGDX so I think I should use the Timer-Utilities. But this is a principal engine-agnostic question since Timer and Interval Classes exits for most languages and frameworks.

My first idea was to just start a countdown and than check everytime in my game loop if the input-variable has changed. If so I will reset the countdown. But this seems like a big overhead because I have to reset the whole timer in worst cases 60 times per second.

Is their a general pattern for that common problem?

\$\endgroup\$
  • 2
    \$\begingroup\$ "But this seems like a big overhead because I have to reset the whole timer in worst cases 60 times per second." I'm sure profiling this will reveal that it's a drop in the ocean of other things your system has to do every frame. \$\endgroup\$ – Vaillancourt Feb 16 '18 at 17:00
2
\$\begingroup\$

If you need to touch it every update anyway, you could simply store a numeric variable that accumulates deltaTime, or a timestamp of the last input, and reset it each time you get input. Then when it exceeds a given value / given duration from "now," trigger your OnIdle beheaviour.

Timers work well when they're fire and forget - none of your code needs to continually check them until they reach their scheduled moment, outside of exceptional cases when you need to cancel them. For something that updates every frame anyway, they don't give any particular advantage over simply polling.

\$\endgroup\$
  • \$\begingroup\$ input frame to current frame delta is what came to mind for me as well \$\endgroup\$ – Stephan Feb 16 '18 at 19:35
0
\$\begingroup\$

Resetting timers is expensive! Instead, keep a single timer firing once a second, and have a variable "seconds_idle" that you increment inside your timer, and can check if >= 60, and can call your "now idle" logic when needed. When you get input, have your inputs reset that variable back to zero, no timer reset needed. That way you're doing cheap, 32-bit unsigned integer operations, instead of the heavier timer manipulation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.