locked
saving and loading files in XNA RRS feed

  • Question

  • Incredibly this topic does not seem to be covered in the XNA help - well it sort of is, but with sample code that doesn't compile and isn't complete - not to mention the helpfiles respond with information about save games instead of actual file handling (ie level data and so forth - the real meat and veg of file handling in a game)

    So here's my question - where do I look in the xna or c# docs for proper samples of saving out a binary or text file containing data I've created myself, and for loading it back in again - from wherever the game is located - I'm not yet interested in saving save-game files from the xbox 360 hard disk. Obviously I've tried typing "file handling", "saving files" and so forth into the xna help but it isn't giving me what I'm after.

    Or, just as likely, I've misunderstood the samples and they in fact can be modified to use the game directory with ease and the compile errors with System.Xml.Serialization not existing are just bad luck on my part.

    Any assistance gratefully received.

    Cheers,

    Robin Jubber

    Sunday, January 7, 2007 1:23 PM

Answers

  • You are going to want to work with "Stream"s specifically FileStream.

    It allows you to work with text/binary files (as well as Memory and Network streaming) both IN and OUT (reading and writing).

    Good luck!

    Sunday, January 7, 2007 2:07 PM
  • That code won't work on 360. You need to find the path to the writable part of the hard disk (for savegames) for this to work.

    To read data relative to the game installation, use the StorageContainer.TitleLocation property.

    To read/write data relative to the savegame location, use StorageDevice, which in turn you get from BeginShowStorageDeviceGuide() (so the user can choose hard disk or game card).

    For more information, see "Storage Overview" in the XNA documentation.

    Sunday, January 7, 2007 9:04 PM

All replies

  • You are going to want to work with "Stream"s specifically FileStream.

    It allows you to work with text/binary files (as well as Memory and Network streaming) both IN and OUT (reading and writing).

    Good luck!

    Sunday, January 7, 2007 2:07 PM
  • It's tough at first for sure. To get System.Xml working you need to right click your Solution name in the Solution Explorer and click 'Add Reference..." Then select System.Xml from the list. Now you will be able to use 'Serialization'.

    Also for information in the Help.  Go to;

    Help-->How Do I-->XNA Game Studio Express-->Programming Guide-->Storage

    Additionally, I'm offering the community a free game with source code to help in the learning process.  Go to my link below and download the game.  In the source code you'll see methods for both saving and loading data.  It was coded for Xbox360 but the code works the same for Windows.
    Sunday, January 7, 2007 3:57 PM
  • Yup - that approach worked nicely

     

    FileStream fs = new FileStream("levels\\level" + level.ToString() + ".SGL", FileMode.Create);

    BinaryWriter w = new BinaryWriter(fs);

    // Write data to file

    for (int i = 0; i < 11; i++)

    {

    w.Write((float) (0.444+(i*10)));

    }

    w.Close();

    fs.Close();

    nice and easy way to write out any kind of data I want. It saved the file next to the executable, which was what I was hoping for. Wonder if this exact same code - well at least the loading files part - will run on the XBOX 360

    Cheers,

    Robin.

    Sunday, January 7, 2007 4:51 PM
  • That code won't work on 360. You need to find the path to the writable part of the hard disk (for savegames) for this to work.

    To read data relative to the game installation, use the StorageContainer.TitleLocation property.

    To read/write data relative to the savegame location, use StorageDevice, which in turn you get from BeginShowStorageDeviceGuide() (so the user can choose hard disk or game card).

    For more information, see "Storage Overview" in the XNA documentation.

    Sunday, January 7, 2007 9:04 PM