Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualEpicWally

Posted 11 September 2012 - 11:56 AM

Hmm, thank you for the tips. So your reccomendation would be to do something to the effect of:

while(playing){
currentTime = System.currentTimeMillis();
if(currentTime > lastTime + updateFrequency){
lastTime = currentTime;
update();
render();
//other game code here
}
}

Did I understand your suggestion correctly? This still looks like it has a potential (albeit small one) for the update frequency to not always be perfect (granted, each cycle where the if statement is false should not take long, so I would doubt you would overshoot your preferred update frequency by more than a ms or two.)

Another thought I had, between posting this and now, would be along a similar line, but still using sleep would do something like:

while(playing){
update();
render();
long currentTime = System.currentTimeMillis();
long sleep = updateFrequency - (currentTime - lastTime);
try{
Thread.sleep(sleep);
}catch(Exception e){}
long lastTime = System.currentTimeMillis();
}

This would allow your delay between cycles to scale based on how long each cycle took, preserving a constant time between cycles (as long as your cycles weren't taking more time than your updateFrequency, which could bugger things up.) I know you said sleep is not the best option, but do you think this would work? My gut feel is that this is cleaner, as your thread is only running when it needs to (once every update cycle) instead of running constantly, continuously checking the system time when it doesn't need to. Not that it's a big overhead. Again, pretty new to this. Just trying to get a good understanding of the pros and cons to all of the possible approaches.

Thanks again for your advice.
-Wally


Edit: I just tried the second idea I mentioned above, and it had the desired effect. I don't know if in a bigger game this would still work as well, or if it only seems to work because of the simplicity of the game.

#2EpicWally

Posted 11 September 2012 - 11:42 AM

Hmm, thank you for the tips. So your reccomendation would be to do something to the effect of:

while(playing){
currentTime = System.getCurrentTimeMilis();
if(currentTime > lastTime + updateFrequency){
lastTime = currentTime;
update();
render();
//other game code here
}
}

Did I understand your suggestion correctly? This still looks like it has a potential (albeit small one) for the update frequency to not always be perfect (granted, each cycle where the if statement is false should not take long, so I would doubt you would overshoot your preferred update frequency by more than a ms or two.)

Another thought I had, between posting this and now, would be along a similar line, but still using sleep... (not sure if this even works, haven't tried it yet.) which would do something like:

while(playing){
update();
render();
int currentTime = System.getCurrentTimeMilis();
int sleep = updateFrequency - (currentTime - lastTime);
try{
Thread.sleep(sleep);
}catch(Exception e){}
lastTime = System.getCurrentTimeMilis();
}

This would allow your delay between cycles to scale based on how long each cycle took, preserving a constant time between cycles (as long as your cycles weren't taking more time than your updateFrequency, which could bugger things up.) I know you said sleep is not the best option, but do you think this would work? My gut feel is that this is cleaner, as your thread is only running when it needs to (once every update cycle) instead of running constantly, continuously checking the system time when it doesn't need to. Not that it's a big overhead. Again, pretty new to this. Just trying to get a good understanding of the pros and cons to all of the possible approaches.

Thanks again for your advice.
-Wally


Edit: I just tried the second idea I mentioned above, and it had the desired effect. I don't know if in a bigger game this would still work as well, or if it only seems to work because of the simplicity of the game.

#1EpicWally

Posted 11 September 2012 - 11:29 AM

Hmm, thank you for the tips. So your reccomendation would be to do something to the effect of:

while(playing){
currentTime = System.getCurrentTimeMilis();
if(currentTime > lastTime + updateFrequency){
lastTime = currentTime;
update();
render();
//other game code here
}
}

Did I understand your suggestion correctly? This still looks like it has a potential (albeit small one) for the update frequency to not always be perfect (granted, each cycle where the if statement is false should not take long, so I would doubt you would overshoot your preferred update frequency by more than a ms or two.)

Another thought I had, between posting this and now, would be along a similar line, but still using sleep... (not sure if this even works, haven't tried it yet.) which would do something like:

while(playing){
update();
render();
int currentTime = System.getCurrentTimeMilis();
int sleep = updateFrequency - (currentTime - lastTime);
try{
Thread.sleep(sleep);
}catch(Exception e){}
lastTime = System.getCurrentTimeMilis();
}

This would allow your delay between cycles to scale based on how long each cycle took, preserving a constant time between cycles (as long as your cycles weren't taking more time than your updateFrequency, which could bugger things up.) I know you said sleep is not the best option, but do you think this would work? My gut feel is that this is cleaner, as your thread is only running when it needs to (once every update cycle) instead of running constantly, continuously checking the system time when it doesn't need to. Not that it's a big overhead. Again, pretty new to this. Just trying to get a good understanding of the pros and cons to all of the possible approaches.

Thanks again for your advice.
-Wally

PARTNERS