Game Development Reference
In-Depth Information
Lines 106 and 142.
Here, both the
LoadGame
and
SaveGame
functions are called
directly for the LoadSaveManager. Notice that both calls construct a valid
path on the system, using the
Application.persistentDataPath
member. The
location of this folder varies from computer to computer, and from operating
system to operating system. But it always points to a valid location on the file
system where data may be stored persistently. It's a good idea, then, to make
use of this system variable whenever saving persistent data to ensure that data
is saved correctly and that your code works across multiple platforms.
Note
More information on
Application.persistentDataPath
can be found in the online Unity
documentation at
https://docs.unity3d.com/Documentation/ScriptReference/Application-
Completing the PlayerController Class
The GameManager now supports the
LoadGame
and
SaveGame
functions. Whenever an object, such
as the main menu, calls these functions via the NotificationsManager, the GameManager will invoke
the appropriate loading and saving behavior implicitly. In fact, if you run CMOD now and test this
code, clicking the save-game button on the main menu should immediately generate a save-game
XML file—a file populated with XML data. This file will be saved in the
persistentDataPath
, which
could be one among a variety of folders on your system, depending on your system configuration.
You can easily find where the folder is located on your computer by using the
Debug.Log
function to
print the
persistentDataPath
variable to the console during Play mode. If you examine the generated
XML file, however, you'll see it's just populated with
default
XML data and not any data related to
the game state specifically. This is because neither the Enemies object nor the Player object ever
populates the
LoadSaveManager.GameStateData
variable. These classes should effectively take action
when receiving a
SaveGamePrepare
event call (to save game data), and a
LoadGameComplete
event call
(to load game data). These events are generated by the LoadSaveManager, shown in Listing 9-5.
So let's now amend the
PlayerController
class first to respond to load and save events in a way
that integrates effectively with the LoadSaveManager. See Listing 9-7 for two new event functions
added to the
PlayerController
class. For brevity and clarity, the rest of the class is not shown here
(the
PlayerController
is defined in Chapter 5). Remember to register the class as a listener for the
two events with the NotificationsManager in the
Start
event. Chapter 3 features more information on
the NotificationsManager, if you need a refresher.
Listing 9-7. Completing the PlayerController Class
01 //------------------------------------------------
02 //Function called when saving game
03
public void SaveGamePrepare(Component Sender)
04 {
05 //Get Player Data Object
06 LoadSaveManager.GameStateData.DataPlayer PlayerData = GameManager.StateManager.
GameState.Player;
07