Skip to main content
C# for Games – Complete Beginner to Advanced Guide
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 the Update 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 Update function runs 60+ times a second.
  • Define Delta Time and 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 massive while(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. 1. Input: Did the player press 'Jump'?
  1. 2. Update: Run C# logic (Apply gravity, move enemies, check collisions).
  1. 3. Render: Draw the resulting 3D/2D graphics to the monitor.
*(If this loop happens 60 times in one second, your game is running at 60 FPS).*

4. The Update Function

In game engines, you don't usually write static void Main(). Instead, engines provide an Update() method.
  • Any code placed inside Update() is automatically called by the engine *every single frame*.
csharp
123456
// In Unity, this looks like:
void Update()
{
    // This will print 60 times a second!
    Console.WriteLine("The game is running!"); 
}

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.
*This is disastrous!* The player on the fast PC runs 5 times faster than the player on the slow PC. The game is "Frame Dependent."

6. The Solution: Delta Time

Delta Time (often written as deltaTime) 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.016 seconds.
  • The Golden Rule: Always multiply movement and timers by deltaTime.
csharp
12345678
// Framerate-Independent Movement
float speed = 50.0f; // 50 pixels per second

void Update()
{
    // By multiplying by deltaTime, the math normalizes based on PC speed!
    player.PositionX += speed * Time.deltaTime;
}

7. Visual Learning: The Game Loop

txt
12345678910111213141516
        +-----------------------------------+
        |                                   |
        v                                   |
[ 1. Check Input ]                          |
(Did user press 'W'?)                       |
        |                                   |
        v                                   |
[ 2. Update Logic (C# Code) ]               |
(Move player forward * DeltaTime)           |
        |                                   |
        v                                   |
[ 3. Render Graphics ]                      |
(Draw the player at the new position)       |
        |                                   |
        +-----------------------------------+
             (Repeats 60+ times a second)

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). A FixedUpdate() or physicsprocess() 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 for loop that searches 10,000 items inside Update(), the CPU is forced to do that calculation 60 times a second. The game will instantly drop to 2 FPS. Only put things in Update() 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
12345678910111213141516171819202122232425262728293031
class Player
{
    float fireCooldown = 0.0f;

    // Called every frame by the game engine
    void Update()
    {
        // 1. Decrease the timer by the exact real-world time passed
        if (fireCooldown > 0)
        {
            fireCooldown -= Time.deltaTime; 
        }

        // 2. Check input
        if (Input.GetButtonDown("Fire"))
        {
            // 3. Check cooldown
            if (fireCooldown <= 0)
            {
                ShootGun();
                fireCooldown = 2.0f; // Lock shooting for 2 real-world seconds
            }
            else
            {
                Console.WriteLine("Gun is reloading...");
            }
        }
    }

    void ShootGun() { Console.WriteLine("BANG!"); }
}

11. Practice Exercises

  1. 1. What does the term "FPS" stand for, and how does it relate to the Game Loop?
  1. 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 the FixedUpdate() loop. Why is the latter required for physics engine calculations?

14. FAQs

Q: Do I have to write the while(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 endless while 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.

16. Next Chapter Recommendation

We know how to update the game over time. It is time to let the player take control. Proceed to Chapter 8: Player Movement and Input Systems.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·