1

This is not super necessary, I am just curious to see what others think. I know it is useless, it's just for fun.

Now I know how to do this, it's fairly simple. I am just trying to figure out a way to do this differently that doesn't require new variables to be created crowding up my class.

Here's how I would do it:

float timePassed = 0f;
public void update(){
    timePassed += deltatime;//Deltatime is just a variable that represents the time passed from one update to another in seconds (it is a float)
    if(timePassed >= 5){
        //code to be ran every 5 seconds
        timePassed -= 5f;
    }
}

What I want to know is if there is a way to do this without the time passed variable. I have a statetime (time since loop started) variable that I use for other things that could be used for this.

halfer
  • 19,824
  • 17
  • 99
  • 186
vedi0boy
  • 1,030
  • 4
  • 11
  • 32
  • Is Spring framework option? – Nenad Bozic Mar 17 '15 at 22:12
  • I want something just in Java. No APIs please. There's got to be some sort of mathematical solution. – vedi0boy Mar 17 '15 at 22:13
  • Is this what you want? http://stackoverflow.com/questions/4544197/how-do-i-schedule-a-task-to-run-at-periodic-intervals – mazaneicha Mar 17 '15 at 22:17
  • Yes it talks about the ScheduledExecutorService that will work for my program. I am just waiting to see if someone has a mathematical solution that does not require running a listener with another thread running. Trying to avoid lag as much as possible since this is for a server/client application. – vedi0boy Mar 17 '15 at 23:59

1 Answers1

2

If the goal really is to run code every X seconds, my first choice would be to use a util.Timer. Another option is to use a ScheduledExecutorService which adds a couple enhancements over the util.Timer (better Exception handling, for one).

I tend to avoid the Swing.Timer, as I prefer to leave the EDT (event dispatch thread) uncluttered.

Many people write a "game loop" which is closer to what you have started. A search on "game loop" will probably get you several variants, depending on whether you wish to keep a steady rate or not.

Sometimes, in situations where one doesn't want to continually test and reset, one can combine the two functions via the use of an "AND" operation. For example, if you AND 63 to an integer, you have the range 0-63 to iterate through. This works well on ranges that are a power of 2.

Depending on the structure of your calling code, you might pass in the "statetime" variable as a parameter and test if it is larger than your desired X. If you did this, I assume that a step in the called code will reset "statetime" to zero.

Another idea is to pass in a "startTime" to the update method. Then, your timer will test the difference between currentTimeMillis and startTime to see if X seconds has elapsed or not. Again, the code you call should probably set a new "startTime" as part of the process. The nice thing about this method is that there is no need to increment elapsed time.

As long as I am churning out ideas: could also create a future "targetTime" variable and test if currentTimeMillis() - targetTime > 0.

startTime or targetTime can be immutable, which often provides a slight plus, depending on how they are used.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • Thanks for the response! I would use the ScheduledExecutorService. I read a little bit about it online an did looks like it would work well. I don't really have plans on resetting statetime (I use it to calculate sprite animation frames). If anyone has a mathematical solution that would be great, if not I will accept this as an answer. I would prefer to avoid using the ScheduledExecutorService but if it's the best way, than I will. – vedi0boy Mar 17 '15 at 23:52