CHAPTER 07
Beginner
Game Loops and Time Management
Updated: May 16, 2026
25 min read
# CHAPTER 7
Game Loops and Time Management
1. Introduction
If you write a standard C# console program, it runs from top to bottom and then immediately shuts down. But a video game does not shut down. It stays open, waiting for you to press buttons, constantly redrawing the screen, and simulating physics. How does it do this? Through the most critical concept in game engine architecture: The Game Loop. In this chapter, we will master the pulse of the game. We will understand theUpdate cycle, learn the critical importance of Delta Time, and ensure our game runs at the exact same speed on a 10-year-old laptop and a massive 4K gaming PC.
2. Learning Objectives
By the end of this chapter, you will be able to:- Explain the architecture of a standard Game Loop (Input, Update, Render).
-
Understand why code placed in an
Updatefunction runs 60+ times a second.
-
Define
Delta Timeand why it is mandatory for movement code.
- Implement framerate-independent movement logic.
- Create simple timer systems using Time variables.
3. What is a Game Loop?
A Game Engine (like Unity or Godot) hides a massivewhile(true) loop behind the scenes. This loop runs endlessly until the player hits "Quit."
Every single cycle of this loop is called a Frame.
The loop always performs three steps:
- 1. Input: Did the player press 'Jump'?
- 2. Update: Run C# logic (Apply gravity, move enemies, check collisions).
- 3. Render: Draw the resulting 3D/2D graphics to the monitor.
4. The Update Function
In game engines, you don't usually writestatic void Main(). Instead, engines provide an Update() method.
-
Any code placed inside
Update()is automatically called by the engine *every single frame*.
csharp
5. The Danger of Frame Dependency
Imagine you write:player.PositionX += 5; inside the Update() function.
- On a slow PC (30 FPS): The loop runs 30 times a second. The player moves 150 pixels per second.
- On a fast PC (144 FPS): The loop runs 144 times a second. The player moves 720 pixels per second.
6. The Solution: Delta Time
Delta Time (often written asdeltaTime) is the exact amount of time, in seconds, it took the computer to process the *previous frame*.
-
If the game is running at 60 FPS, Delta Time is roughly
0.016seconds.
-
The Golden Rule: Always multiply movement and timers by
deltaTime.
csharp
7. Visual Learning: The Game Loop
txt
8. Best Practices
-
Use FixedUpdate for Physics: Most game engines have two update loops. The standard
Update()runs as fast as the monitor allows (fluctuating). AFixedUpdate()orphysicsprocess()runs at a locked, steady interval (e.g., exactly 50 times a second). *Always* put RigidBody physics calculations in the Fixed update to prevent collision glitches!
9. Common Mistakes
-
Putting Heavy Logic in Update: If you put a massive
forloop that searches 10,000 items insideUpdate(), the CPU is forced to do that calculation 60 times a second. The game will instantly drop to 2 FPS. Only put things inUpdate()that absolutely *must* happen every frame (like moving a character).
10. Mini Project: Build a Cooldown Timer
Objective: Use Delta Time to create a real-time countdown timer. *(Note: Because standard Console apps don't have a built-in game loop, this is a conceptual example of how you would write this in a game engine like Unity).*
csharp
11. Practice Exercises
- 1. What does the term "FPS" stand for, and how does it relate to the Game Loop?
- 2. What is the mathematical definition of Delta Time?
12. MCQs with Answers
Question 1
You write enemy.health += 1; inside the Update() function. What will happen to the enemy's health?
Question 2
Why MUST you multiply movement speed by deltaTime in a game engine?
13. Interview Questions
- Q: Explain the architecture of a standard Game Loop. What are the three primary phases that occur during a single frame?
- Q: A player playing your game on a 144Hz monitor complains that their character runs twice as fast as their friend playing on a 60Hz monitor. What programming concept did you forget to implement, and how do you fix it?
-
Q: Contrast the standard
Update()loop with theFixedUpdate()loop. Why is the latter required for physics engine calculations?
14. FAQs
Q: Do I have to write thewhile(true) game loop myself?
A: No! If you use Unity, Unreal, or Godot, the engine's core C++ code handles the complex while loop, thread management, and rendering. You simply drop your C# code into the provided Update() methods, and the engine calls it for you.
15. Summary
In Chapter 7, we discovered the heartbeat of the game engine. We learned that games are endlesswhile loops that process input, update logic, and render graphics dozens of times a second. We recognized the extreme danger of Frame Dependency, and learned the industry-standard solution: multiplying our math by Delta Time to ensure our game runs flawlessly and consistently across any hardware.