Save and Load Game Data

Apple Game Frameworks and Technologies — by Tammy Coron (144 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Present the Game Over Scene | TOC | Exit the Map and Load the Next Scene 👉

Saving and loading game data is essential for games like Val’s Revenge. Players don’t want to spend a lot of time progressing through a game if their progress isn’t saved.

But saving and loading game data isn’t something you can take lightly. You need to consider what data to save and how to save it.

For a game like Val’s Revenge, you’ll need to save the current level, the number of keys collected, and how much treasure the player found. Because you don’t want nefarious users to “break into” this saved data and make changes, you will securely store it to the file system rather than using NSUserDefaults.[44]

To help speed things up, you’ll be adding a pre-made GameData class.

Open Finder and in the resources folder for this chapter, you’ll see a GameData.swift file. Drag that file into the Project Navigator, placing it just below the GameViewController.swift file. Don’t forget to verify that the Copy items if needed and Add to targets options are both checked. Also make sure that the option to Create groups is selected.

The GameData.swift file is small, but it packs a lot of punch. If you don’t already have it open in the Source Editor, open it now.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.