- Sloth Bytes
- Posts
- 𦄠How Does Game Saves Work?
𦄠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.
The challenges that stem from using manual code review processes or the wrong AI tools
Why AI-powered code review is crucial for improving code quality, reducing reviewer fatigue and maintaining company standards.
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.
Unity has a system called playerPrefs
Godot has a system called config files
Unreal Engine seems to have the most complete/complex save system.
If you plan on publishing your game on Steam, you can use their cloud save system
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!
andregarcia0412, joshymerki, Tajgero, GabrielDornelas, FouadNara, xanerin, graff012, Better-Canada, Cariburi, JamesHarryT, GaLinux, AspenTheRoyal, Fireboy086, mau-estradiote, spenpal, dganesh05, xarop-pa-toss, Franspi-lol, RelyingEarth87, Moizg, and cuisse!
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!
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? |
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