Game Development Reference
In-Depth Information
32 //Call load end notification
33 GameManager.Notifications.PostNotification(this, "LoadGameComplete");
34 }
35 //-----------------------------------------------
Line 03.
The
Save
function provides a default function argument, which
makes it optional. This means two call types are valid here, namely
Save()
and
Save("
MyFileName
")
. The former call will resort to the default string of
"GameData.xml"
as the argument.
Lines 5-15.
The
Save
function begins by clearing out any Enemy data from
the internal arrays, in case a previous game was saved. Then, the class
XMLSerializer
is instantiated, passing a valid instance to
GameStateData
in the
constructor, indicating the class to serialize. The call on line 14 actually serializes
the data in the class to a file stream. And then, finally, the data is committed to
the file and the file is closed using a
Stream.Close
call at line 15.
Lines 22-34.
The
Load
function is essentially the inverse of the
Save
function.
This time an
XMLSerializer
is instantiated to
Deserialize
an XML file
stream back to the
GameStateData
class, allowing the game state data to be
reconstructed from a file.
Lines 01-35.
Notice that throughout the load and save process, the
NotificationsManager is updated by event calls. This will be important later, as
other classes (such as the Player and Enemies) will respond to these invents to
load back their data from the
GameStateData
object.
Completing the GameManager Class
The
LoadSaveManager
class is now completed. But on its own it can achieve nothing; since none of
its functionality is integrated into the main game logic or mechanics. Now it's time to connect the
LoadSaveManager into the GameManager to start making things work together. To start, drag and
drop the LoadSaveManager script from the Project panel onto the
GameManager
object in the scene
to add a LoadSaveManager component to the object (see Figure
9-4
).