XNa: saves data to files and reads data from files

Source: Internet
Author: User

Based on the latest xNa game studio 4.0 environment, some methods have changed because of the changes in versions earlier than xNa game studio 4.0.

Premise: You have a correct storagedevice and defined the data to be saved.

[Serializable]
Public   Struct Savegamedata
{
Public   String Playername;
Public Vector2 avatarposition;
Public   Int Level;
Public   Int Score;
}

1. to serialize data to a save game file

1. Create a storagecontainer to access the specified device.

Code

// Open a storage container.
Iasyncresult result = Device. beginopencontainer ( " Storagedemo " , Null , Null );
// Wait for the waithandle to become signaled.
Result. asyncwaithandle. waitone ();
Storagecontainer container = Device. endopencontainer (result );
// Close the wait handle.
Result. asyncwaithandle. Close ();

2. Call fileexists to determine if an earlier save game file exists, and if it does, call deletefile to delete it.

String Filename =   " Savegame. Sav " ;
// Check to see whether the Save exists.
If (Container. fileexists (filename ))
// Delete it so that we can create one fresh.
Container. deletefile (filename );

3. Create a stream object on the file by using the createfile method.

// Create the file.
Stream stream = Container. createfile (filename );

4. Create an xmlserializer object, passing the type of the structure that defines your save game data.

// Convert the object to XML data and put it in the stream.
Xmlserializer serializer =   New Xmlserializer ( Typeof (Savegamedata ));

5. Call serialize, and then pass the stream and the data to serialize.

The xmlserializer converts data in the structure to XML, and uses the stream to write the data into the file.

Serializer. serialize (stream, data );

6. Close the stream.

// Close the file.
Stream. Close ();

7. dispose the storagecontainer to commit the changes to the device.

// Dispose the container, To Commit Changes.
Container. Dispose ();

II. To read serialized data from a save game file

1. Create a storagecontainer to access the specified device.

Code

// Open a storage container.
Iasyncresult result = Device. beginopencontainer ( " Storagedemo " , Null , Null );
// Wait for the waithandle to become signaled.
Result. asyncwaithandle. waitone ();
Storagecontainer container = Device. endopencontainer (result );
// Close the wait handle.
Result. asyncwaithandle. Close ();

2. Call fileexists to determine if the save game exists.

Code

String Filename =   " Savegame. Sav " ;
// Check to see whether the Save exists.
If ( ! Container. fileexists (filename ))
{
// If not, dispose of the container and return.
Container. Dispose ();
Return ;
}

3. Open a stream object on the file by using the openfile method.

// Open the file.
Stream stream = Container. openfile (filename, filemode. Open );

4. Create an xmlserializer object, and then pass the type of the structure that defines your save game data.

Copyxmlserializer serializer =   New Xmlserializer ( Typeof (Savegamedata ));

5. Call deserialize, and then pass the stream object.

Deserialize returns a copy of The save game structure populated with the data from the save game file. (You will have to cast the return value from object to your type .)

Savegamedata data = (Savegamedata) serializer. deserialize (Stream );

6. Close the stream.

// Close the file.
Stream. Close ();

7. dispose the storagecontainer.

// Dispose the container.
Container. Dispose ();

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.