Questions tagged [game-loop]

The central code loop responsible for handling the running gameplay. At its most basic state, it accepts input, resolves the actions of entities, and renders the scene.

The game loop is designed to ensure the persistence of action in a game. Unlike many other software applications, games do not stall and wait for input. The world continues to run while the player watches, which importantly allows timing to play a role.

The game loop is responsible for allowing this, by continually rendering the scene without requiring the player's input. A rudimentary game loop might run like the following:

  1. Accept player input if any exists, resolve actions for player as a result of any input
  2. Resolve AI decisions and actions
  3. Determine collisions and resolve their impact
  4. Render the resulting scene

Use this tag when you're asking questions such as the proper structure, optimization and efficiency strategies, or debugging errors in the main game loop code.

489 questions
270
votes
11 answers

When should I use a fixed or variable time step?

Should a game loop be based on fixed or variable time steps? Is one always superior, or does the right choice vary by game? Variable time step Physics updates are passed a "time elapsed since last update" argument and are hence framerate-dependent.…
Nick Sonneveld
  • 2,849
  • 3
  • 19
  • 9
137
votes
1 answer

What should a main game loop do?

Generally , what are the core things that one should do in the game loop, and what are some things that one shouldn't do in the game loop?
hokiecsgrad
  • 2,498
  • 3
  • 21
  • 14
87
votes
5 answers

What is the point of update independent rendering in a game loop?

There are dozens of articles, books and discussions out there on game loops. However, I pretty often come across something like this: while(running) { processInput(); while(isTimeForUpdate) { update(); } render(); } What basically is bothering me…
Sam
  • 843
  • 1
  • 7
  • 6
42
votes
5 answers

Should actors in a game be responsible for drawing themselves?

I am very new to game development, but not to programming. I am (again) playing around with a Pong type game using JavaScript's canvas element. I have created a Paddle object which has the following properties... width height x y colour I also have…
alex
  • 753
  • 1
  • 9
  • 17
41
votes
3 answers

What is the standard C# / Windows Forms game loop?

When writing a game in C# that uses plain-old Windows Forms and some graphics API wrapper like SlimDX or OpenTK , how should the main game loop be structured? A canonical Windows Forms application has an entry point that looks like public static…
user1430
39
votes
3 answers

When several classes need to access the same data, where should the data be declared?

I have a basic 2D tower defense game in C++. Each map is a separate class which inherits from GameState. The map delegates the logic and drawing code to each object in the game and sets data such as the map path. In pseudo-code the logic section…
Juicy
  • 581
  • 2
  • 6
  • 8
35
votes
2 answers

A good way to build a game loop in OpenGL

I'm currently beginning to learn OpenGL at school, and I've started making a simple game the other day (on my own, not for school). I'm using freeglut, and am building it in C, so for my game loop I had really just been using a function I made…
Jeff
  • 2,478
  • 4
  • 24
  • 29
29
votes
2 answers

Is it safe to use Sleep() in game loop (on Windows)?

Is it safe to use Sleep() function on Windows in game loop (C++)? I want to have fixed frame rate.
hemogoblin
  • 291
  • 1
  • 3
  • 3
24
votes
4 answers

What is the point of "delta" in this code? Does it reflect a standard thing in game dev?

I know very little about game programming but would like to learn more. I am trying to understand the code for this game. I am trying to understand why the code is passing a "delta" to Shipcontrols.js, which changes the direction of the ship based…
bernie2436
  • 425
  • 3
  • 10
20
votes
5 answers

Game actions that take multiple frames to complete

I've never really done much game programming before, pretty straightforward question. Imagine I'm building a Tetris game, with the main loop looking something like this. for every frame handle input if it's time to make the current block move down a…
user5438
20
votes
4 answers

Is there any harm in having the main game loop run uncontrolled?

I was wondering if there is any possible harm when my game loop runs as fast as the system allows? I currently have a loop, that, by measuring passed time in nanoseconds, runs the game logic and rendering logic at predefined speeds without a…
dot_Sp0T
  • 2,062
  • 2
  • 16
  • 25
20
votes
2 answers

Where should collision detection logic be placed?

I am developing a small 2D game engine. The characters have a paint method which currently does the following: Calculate the new position of the character as per its speed, etc. Update the collision grid cell ** Draw the character at the new…
Cracker
  • 497
  • 4
  • 10
19
votes
1 answer

Behavior Trees :: Actions That Take Longer Than One Tick

From what I understand on Behavior Trees, each Behavior should be a short goal oriented Action that could be done in a few iterations. So for example, below is an image of a Behavior Tree: Now let us assume that the Drive To Enemy behavior takes…
Free Lancer
  • 1,283
  • 1
  • 13
  • 18
18
votes
3 answers

Fixed timestep game loop, why interpolation

This is a very standard way of doing a fixed timestep game loop, where you just accumulate time from the previous frame and consume it in discrete steps with the physics engine: while ( accumulator >= dt ) { previousState = currentState; integrate(…
Gavriil
  • 193
  • 1
  • 6
18
votes
2 answers

Why does my sprite player move faster when I move the mouse?

I'm trying to develop a simple game made with Pygame (Python library). I have a sprite object which's the player and I move it using arrow keys. If I don't move the mouse, the sprite moves normally, but when I move the mouse, the sprite moves faster…
Drumnbass
  • 295
  • 3
  • 9
1
2 3
…
32 33