Game Development Reference
In-Depth Information
Lines 31-55. Both the Player and Enemy classes use DataTransform to store
their transformation data.
Line 65. The GameStateData class consolidates all Player and Enemy data in the
scene using serializable types. An instance of this class ( GameState ) is declared
as a public member of the LoadSaveManager class and will be later serialized to
an XML file.
Loading from and Saving to an XML File
The LoadSaveManager class is managerial, insofar as it oversees the general loading and saving
process from and to an XML file. This class features a critical member, namely GameState , which
consolidates all the serializable game data to be saved to a file and loaded from a file. Effectively, the
GameState member will represent a game state in memory, as we'll see later. The question then arises
as to how this class can be saved to XML and loaded from XML. To achieve this, the LoadSaveManager
class be amended with two new functions. These are listed in Listing 9-5. Comments follow.
Listing 9-5. Adding Load and Save Functionality into the LoadSaveManager Class
01 //-----------------------------------------------
02 //Saves game data to XML file
03 public void Save(string FileName = "GameData.xml")
04 {
05 //Clear existing enemy data
06 GameState.Enemies.Clear();
07
08 //Call save start notification
09 GameManager.Notifications.PostNotification(this, "SaveGamePrepare");
10
11 //Now save game data
12 XmlSerializer Serializer = new XmlSerializer(typeof(GameStateData));
13 FileStream Stream = new FileStream(FileName, FileMode.Create);
14 Serializer.Serialize(Stream, GameState);
15 Stream.Close();
16
17 //Call save end notification
18 GameManager.Notifications.PostNotification(this, "SaveGameComplete");
19 }
20 //-----------------------------------------------
21 //Load game data from XML file
22 public void Load(string FileName = "GameData.xml")
23 {
24 //Call load start notification
25 GameManager.Notifications.PostNotification(this, "LoadGamePrepare");
26
27 XmlSerializer Serializer = new XmlSerializer(typeof(GameStateData));
28 FileStream Stream = new FileStream(FileName, FileMode.Open);
29 GameState = Serializer.Deserialize(Stream) as GameStateData;
30 Stream.Close();
31
 
Search WWH ::




Custom Search