🦄 How Does Game Saves Work?

Hello friends!

Welcome to this week’s Sloth Bytes. I hope you had a silly week.

How Playrix increased developer productivity and code quality with AI-powered Code Reviews

Learn how Korbit AI enabled one of the world’s top gaming companies to accelerate engineering velocity and improve code quality.

  1. The challenges that stem from using manual code review processes or the wrong AI tools

  2. Why AI-powered code review is crucial for improving code quality, reducing reviewer fatigue and maintaining company standards.

  3. How Korbit accelerates the SDLC process for hundreds of enterprises with instant, AI-powered code reviews and powerful insights into the codebase, projects and team.

🦄 How Games Remember Progress

Someone asked to do a game dev topic and I don’t have much game dev experience, so if I get something wrong, let me know!

If you’ve ever played any modern game, you’ve seen the words ā€œsavingā€¦ā€ or ā€œauto saveā€ somewhere.

But how the heck does that even work?

What exactly is it saving and when does it know to save?

I was very curious about that and did some research, and it’s a pretty cool concept.

I’ll be writing these examples in python to make it easier to read 😁 

What do games save?

Games usually save important state data, not specific details:

# Bad: Everything
save_data = {
    "player_x": 143.23842,
    "player_velocity_x": 0.0023,
    "current_animation_frame": 3,
    "player_y" = -0.4314324
    "time_since_last_blink": 1.34,
    # ... 500 more random fields
}

# Good: What matters
save_data = {
    "level": 5,
    "health": 80,
    "inventory": ["sword", "potion"],
}

How do game save?

Games have a lot of different methods to saving:

Method 1: JSON (Text Files)

Very easy to implement and I think it’s the most common approach.

It works great as a simple save system:

import json

def save_game():
    data = {
        "level": player.level,
        "health": player.health,
        "inventory": player.inventory
    }
    with open("save.json", "w") as f:
        json.dump(data, f)

def load_game():
    with open("save.json", "r") as f:
        data = json.load(f)
        player.level = data["level"]
        # ... restore rest

āœ… Human-readable and easy to debug.
āŒ Easy to cheat if you don’t encrypt and the files are larger.

Method 2: Binary Files

For performance and smaller files, you can convert your data into binary:

import struct

def save_binary():
    with open("save.dat", "wb") as f:
        f.write(struct.pack('i', player.level))    # 4 bytes
        f.write(struct.pack('f', player.x))        # 4 bytes
        f.write(struct.pack('i', len(inventory)))  # 4 bytes
        
        for item in inventory:
            f.write(struct.pack('i', item.id))

def load_binary():
    with open("save.dat", "rb") as f:
        player.level = struct.unpack('i', f.read(4))[0]
        player.x = struct.unpack('f', f.read(4))[0]
        # ... etc

Same data: 200 bytes (JSON) → 20 bytes (binary)

āœ… Fast, small, harder to edit
āŒ Not human-readable, harder to test, easy to corrupt

Method 3: Databases

If you have large/complex data, You can use a database to save:

import sqlite3

conn = sqlite3.connect('save.db')
c = conn.cursor()

# Save
c.execute("INSERT INTO player VALUES (?, ?, ?)",
          (player.level, player.health, player.x))

c.execute("INSERT INTO inventory VALUES (?, ?)",
          (item_id, quantity))

conn.commit()

āœ… Complex queries, unlimited slots
āŒ Overkill for simple games

Platform-Specific

Some game engines/platforms have their own implementation/APIs, but you can also use the other methods I mentioned.

Fun fact: The original Zelda (1986) pioneered battery-backed saves on cartridges. Before that, games used password systems.

Imagine entering a 50-character password to restore your Skyrim character.

Thanks for the feedback! I’ll try to do a easy + hard challenge.

Wow a lot of submissions this week. I guess it was too easy…

Thanks to everyone who submitted!

Spiral Matrix

Given a matrix of m * n elements (m rows, n columns), return all elements of the matrix in spiral order.

Examples

spiralOrder([
  [ 1, 2, 3 ],
  [ 4, 5, 6 ],
  [ 7, 8, 9 ]
])
output = [1, 2, 3, 6, 9, 8, 7, 4, 5]

spiralOrder([
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
])
output = [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]

How To Submit Answers

Reply with

  • A link to your solution (github, twitter, personal blog, portfolio, replit, etc)

  • or if you’re on the web version leave a comment!

  • If you want to be mentioned here, I’d prefer if you sent a GitHub link or Replit!

New video about useful GitHub Repos!

16 Cool GitHub Repos You WILL Use (no pressure)

That’s all from me!

Have a great week, be safe, make good choices, and have fun coding.

If I made a mistake or you have any questions, feel free to comment below or reply to the email!

See you all next week.

What'd you think of today's email?

Login or Subscribe to participate in polls.

Want to advertise in Sloth Bytes?

If your company is interested in reaching an audience of developers and programming enthusiasts, you may want to advertise with us here.

Reply

or to participate.