Time, delays, etc

  Since I've started programming in the first place I have always had an integer variable that would be used to trigger something at a certain moment. So in an infinite loop I would have something like 


    timeVariable++;
  If (timeVariable >= 500){
       doSomething ();
       timeVariable -= timeVariable;
}

The problem is I never learned how to properly keep track of time and I fear that the faster a computer is the faster this will trigger. I have always used this to create bullets, rain, etc. Would anyone mind showing me a proper way to keep track of time to trigger events (preferably in libgdx)?

Good suggestion. I’d like to add that there is also the option of a ConcurrentSkipListSet, where you could use your timestamp a Comparator. Part of the point is that this way one can have the set be invoked in the “game loop” but at the same time have asynchronous events, such as things driven by mouse or key actions, be added into the queue concurrently rather than as part of the game loop.

Why you use Runnable ?)

edit: Runnable mind me wrong Way XD

Simplest solution: If you limit your update loop to X updates per second, you can wait (delay * X) ticks until you fire off the event.

I have a variable in my game loop keeping track of the current system time every frame. So if you fire a bullet, take that time, and don’t let them fire another bullet until lets say, one second has passed.