Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

How to make an economical main game loop (essential for big games)

game design loops

  • Please log in to reply
9 replies to this topic

#1 Exabit

Exabit

    CC Newcomer

  • Member
  • PipPip
  • 22 posts
  • Location:Sydney Australia
  • Programming Language:Java
  • Learning:Python

Posted 17 January 2013 - 09:58 PM

Most people think that when making a game you have, at the center of it a main game loop that runs as fast as it can to update the display and objects in the game.
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.
  • 0

#2 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 18 January 2013 - 03:05 AM

Hey, thanks for the post and all, but you should probably work on your identation.

Here's the code in C++ that serves the same purpose:

bool run = true;
const int frames_per_second = 60;
const int updates_per_second = 100; //Should not be lower than the fps.
int last_update_timestamp = 0; //Records when the last update call happened.
int last_render_timestamp = 0;
int current_time = 0; //Updated at the beginning of every update loop. Not to be mixed with world time.

while (run)
{
    current_time = media.GetMs();
    
    media.HandleEvents(&run);
    
    if (1000.0/frames_per_second <= current_time - last_render_timestamp)
    {
	    media.Render(world);
	    last_render_timestamp = current_time;
    }
    
    if (1000.0/updates_per_second <= current_time - last_update_timestamp)
    {
	    world.Update();
	    last_update_timestamp = current_time;
    }
}

  • 1

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.


#3 Exabit

Exabit

    CC Newcomer

  • Member
  • PipPip
  • 22 posts
  • Location:Sydney Australia
  • Programming Language:Java
  • Learning:Python

Posted 18 January 2013 - 03:38 AM

Thanks, sorry about the mess.

One question, why FPS and UPS. The point of the game is to update in a certain amount of time but why have the fps (I find that displaying is less resource intensive than calculation)
  • 0

#4 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 20 January 2013 - 02:45 AM

That loop is not actually from a game, but something else, that doesn't require rendering by default at all. For a game, you might still need to update more frequently because your physics could require that, but you're probably good at 60 UPS.
  • 0

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.


#5 Exabit

Exabit

    CC Newcomer

  • Member
  • PipPip
  • 22 posts
  • Location:Sydney Australia
  • Programming Language:Java
  • Learning:Python

Posted 20 January 2013 - 02:54 AM

I dont think you understand.
A game renders every certain period of time and changes state every certain period of time.
FPS:UPS
Why not have them in the same loop.
>Faster
>Less complicated
>Less error prone
??
  • 0

#6 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 20 January 2013 - 03:19 AM

But I do have it in the same loop.

The only odd thing about my loop is that it updates the simulation more frequently than it renders it. Why? First, rendering at more than 60 FPS is wasting resources. Second, my simulation will mabye have to be updated 1000 times per second, or more, for anything noticable to happen. It is not a game. Currently it's at 100 UPS because I haven't yet implemented most of the simulation, so I haven't experimented with update intervals.

Or were you asking something else?
  • 0

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.


#7 Exabit

Exabit

    CC Newcomer

  • Member
  • PipPip
  • 22 posts
  • Location:Sydney Australia
  • Programming Language:Java
  • Learning:Python

Posted 24 January 2013 - 03:50 AM

I am asking why you do not do everything in one loop. Your code contains two loops, one for fps and one for ups. This means that you display the game three out of five times it updates.

As games are display based is this not a waste?
Why not display and update in the same loop. You still have a steady fps/ups but you use less resources and the code is easier to follow.
  • 0

#8 Yannbane

Yannbane

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 238 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, PL/SQL, Lisp, Assembly, Bash, Others
  • Learning:Lisp, Scheme

Posted 26 January 2013 - 03:48 AM

That's only true for games, but the loop I had posted is not for a game.

Read my post again, I'm not creating a game at all, but something that will most certainly have a very fast update interval.

If I rendered and updated my simulation at the same time, I would have to render it more that 60FPS, maybe at 1000FPS, which is not possible and definitely not efficient.

Also, I don't have "two loops". There is only one loop, and two events that go off at different intervals.
  • 0

My blog: yannbane.com. I post about programming, game development, and artificial intelligence.


#9 Drakhtul

Drakhtul

    CC Lurker

  • New Member
  • Pip
  • 5 posts
  • Programming Language:(Visual) Basic
  • Learning:(Visual) Basic

Posted 09 February 2013 - 12:05 PM

thanks for the tut :)


  • 0

#10 JackFrost

JackFrost

    CC Newcomer

  • Member
  • PipPip
  • 24 posts
  • Location:United Kingdom
  • Programming Language:PHP, JavaScript, PL/SQL, Visual Basic .NET
  • Learning:C, Java, C++, PHP

Posted 15 May 2013 - 06:51 AM

Yannbane could having separate code for both updates and frames be useful in games as well?

 

As you say going above 60 fps is normally a waste of resources but if there is stuff going on in the background that isn't necessarily shown on the screen would it be worth having separate rates for updating and rendering? Could it not mean the game could "simulate" what is going on more accurately even if the frames don't show all that is happening?

 

I can see this leading to problems, for example if a "soldier" in the game dies before it is rendered even if only by 10th of a second it could be noticeable but for things that aren't necessarily shown (hunger or spread of a disease?) it could help simulate them without wasting resources updating the screen?

 

Does anyone have any experience with this in a gaming environment?

 

And finally looking at your code the screen would be updated and then the simulation made, would this lead to "lag" between what is happening and what is shown if both the render and simulation are complex methods that take time to calculate? Is this noticeable in your program?


  • 0





Also tagged with one or more of these keywords: game design, loops