This is mainly true but there is a big problem with updating as fast as you can.
1. Uses up unnecessary amounts of resources.
2. You are forced to make the game run at a reasonable pace by waiting in the update code or in the main game loop with timers. This is a waste of computing power and often leads to crashing games.
Wont work: (Java)
public class Main {
public static void main(String [] args) {
while(true) {
game.update();
wait(10);
}
}
}
To fix this you can run the game at a designated ticks per second rate. So even if the game runs behind it uses all it needs without disabling threads. If it runs ahead it simply waits for the next tick.
Will work: (Java)
public class Main {
private static boolean tickDone = true;
private static long oldTime = System.nanoTime() /1000000; //Gets time in milliseconds, more accurate than System.currentTimeMillis()
private static int ticksPerSecond = 200;
public static void main(String [] args) {
Game game = new Game();
while(true) {
if(tickDone) {
tick(System.nanoTime() /1000000, game);
}
}
}
private void tick(long inputTime, Game game) {
tickDone = false;
while(inputTime > oldTime) {
game.update();
oldTime = oldTime + (1000/ticksPerSecond); //Every tick has a time in which it should be done. In this case 5 ms. This adds five ms's to time until it is up to date
}
tickDone = true;
}
}
This code means that the ticks will run at a steady rate. Each time the tick method is run the time is either less or more than what the game is up to. If less then the game runs until it is more. When more it declares the tick complete and waits for it to be less. If each object is given a set speed per tick then this enables the game to use minimal resources and doesnt waste what it is given by sleeping in a tick.
Any questions will be answered.
















