Game Development Reference
In-Depth Information
method is right for you. If you're interested in saving games with the BinaryFormatter , a Unity video
tutorial is available at https://unity3d.com/learn/tutorials/modules/beginner/live-training-
archive/persistence-data-saving-loading .
Note File-based saving works well for many deployment types, but for web-player games and web-hosted
games, persistent data becomes more troublesome due to security restrictions. There are several main
options available: 1) Game data can be saved in a server-side database. 2) Game data can be stored inside
a cookie file. 3) Data can be stored in a single XML string that can be passed as a query to the web player.
Further details for saving data with web-based deployments are beyond the scope of this topic.
Saving with XML
Saved games for CMOD will be saved in the XML format. XML stands for E X tensible M arkup
L anguage, and it uses a hierarchical text-based structure for saving data. There are alternative
text-based formats available, which are gaining popularity in games today. One is JSON (JavaScript
Object Notation), which features a more abbreviated and slim-line syntax than XML, making it a
particularly attractive option for streaming text-based data across networks. But currently, JSON
is not supported natively by Unity or the Mono Framework (as of version 4.3). This means that you
must rely on custom-made parsers or third-party parsers to read from and write to JSON files. For
CMOD and this topic in general, I stick to the native tools and classes that work out-of-the-box with
Unity. Hence, I'll choose the XML file format, which is powerful, versatile, and long established.
Note A freely available third-party class for parsing JSON data can be found at http://wiki.unity3d.
com/index.php/SimpleJSON . When using third-party code, be sure to read the source file comments and
summary thoroughly.
Let's now take a look at the saved-game XML file structure to be used for CMOD. In sum, there are
several data items we'll need to save to offer complete load-and-save functionality. Specifically,
for the Player, we'll need to save transformation (position, rotation, and scale data), collected cash ,
collected weapon (if any), and health . For the Enemy, we'll store transformation , unique ID (to identify
the Enemy type), and health . This data can be consolidated into a single XML file, as shown in
Listing 9-3 (this sample features only one Enemy alongside Player data for the sake of brevity—though
the “real” file will feature more Enemies).
Listing 9-3. Sample XML Data for a CMOD Saved Game
<?xml version="1.0" encoding="Windows-1252"?>
<GameData xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:xsd=" http://www.w3.org/2001/
XMLSchema " >
<Enemies>
<DataEnemy>
<PosRotScale>
<X>1.94054472</X>
 
Search WWH ::




Custom Search