Press "Enter" to skip to content

Lesson 18.5: Update game loading and saving

Now we can update the save and load game functions.

I originally added versioning, so we can automatically upgrade save game files as we release new versions of the game, with different saved game file formats. But, if we keep doing that while making many significant changes, we’re going to add “update save and load functions” to many lessons.

So, for now, I’m going to remove the versioning. Once the game is more stable, we’ll add it back in.

Step 1: Modify Engine\Services\SaveGameService.cs Start by adding “using System.Collections.Generic” as a new using directive. Uncomment the LoadLastSaveOrCreatenew function and replace the “return new GameSession();”on lines 24 and 43 with throwing exceptions. We don’t want to allow starting the game with a default Player object.

Uncomment the code for the CreatePlayer function and delete the lines that use the file version. On line 54 replace the code that used to set the Dexterity with a call to GetPlayerAttributes – a new function we’ll create. On the final line of this function, replace the “return null;” with “return player;”.

On lines 66-82, add the new GetPlayerAttributes function to read the PlayerAttribute values from the saved game file and create PlayerAttribute objects.

In the PopulatePlayerInventory, PopulatePlayerQuests, and PopulatePlayerRecipes functions, remove the code that works with file versions.

Finally, delete the FileVersion function at the end of the class.


Step 2: Modify WPFUI\MainWindow.xaml and MainWindow.xaml.cs In MainWindow.xaml.cs, I added two optional parameters to the constructor for the starting location coordinates.

For new games, we won’t have coordinates and will start the player at location 0, 0. For saved games, we will have values to pass in.

In StartNewGame_OnClick (line 140), we’ll display a Startup window to let the user either create a new player or load a game from a saved game file.

Since we’re going to delete the “Load Game” menu option, and handle that through the “Start new game” menu option displaying the Startup window, we can delete the LoadGame_OnClick function that started on line 145.

In MainWindow_OnClosing, I moved its code to a separate AskToSaveGame function. In the future, I’d like to expand that a bit to check if the player did anything (moved location, changed inventory, learned a new recipe, etc.). If the player hasn’t done anything, we won’t ask to save the game – since nothing has changed.

Finally, I uncommented the SaveGameService.Save on line 186, so the play can save the game.

In MainWindow.xaml, I removed the IsEnabled=False lines from the start, load, and save game MenuItems.

I changed the “Start New Game” text to “New Game” on line 36, deleted the Separator on line 39, and deleted the “Load Game” MenuItem – since it’s handled in the Startup window called by the “New Game” MenutItem.



Step 3: Modify WPFUI\Startup.xaml and Startup.xaml.cs We’ll add the ability to load a game from a saved game file to the Startup window.

We’ll start by updating the using directives for Startup.xaml.cs.

I added a constant for the saved game file extension to line 11 and deleted the _gameDetails backing variable (because we only used it in one place).

In the constructor, I removed storing the GameDetails to the private variable we also removed and just store it directly into the DataContext.

On lines 27-49, there’s a new function to load a saved game from disk.

It creates an OpenFileDialog box to let the user browse for the saved game file. If they select a file, we create a GameSession object from the file on lines 38-39. Then, we pass in the GameSession’s Player and location information to a new MainWindow game, to start playing the game.

In the Startup.xaml, we add a new row to the grid on line 17, then add the new “Load saved game” button on lines 30-35.

I also changed the text for “Start New Game” to “Start new game”, just because I think it looks a little nicer that way.



Step 4: Delete TestEngine\Services\TestSaveGameService.cs and TestEngine\TestFiles\SavedGames\Game_0_1_000.soscsrpg We’ll add unit tests back in once we start working with different versions of saved game files.

Leave a Reply

Your email address will not be published. Required fields are marked *