www.xbdev.net xbdev - software development
Friday December 14, 2018
home | about | contact | Donations

     
 

XNA & 360

The bits and bytes of XNA and C# for your 360...

 

Its not as fun as drawing to the screen or making your controller vibrate, but its still needed!  As sooner or later you'll need to write/read files for your 360 code....Bit more code than I thought...but I'm sure once you come to grips with the xbox360's quirks you'll be fine.

 

Writing and Reading data on your XBOX360 requires a few hoops to jump through.  You can't simple go opening a file and writing to it, as you need to specify a valid filepath and the only way of doing this is through the StorageContainer API.  Some simple steps to create a storage device and write your file are show below.

 

  • Add System.IO namespace, need this to use the standard Write/Read Methods

  • Add GameServiceComponent service to your constructor, so you can use the StorageDevice

  • Write a StorageDeviceSelected Method, that gets called by the API when you can write your data

  • Call BeginStorageDeviceContainer when you want to read/write your data!

 

For the example below, when you press the 'X' button on the 360 it will trigger the code to actually write a file called 'myfile.txt' to your storage area.  You could also add some code which instead of creating and writing to that file, you could check if it already exists using the 'File.Exists(pathAndFilename);' Member function.

 

GameClass.cs [Download Full SourceCode]
using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Storage;

 

//**1**

//**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

//**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

using System.IO;

 

public class GameClass : Microsoft.Xna.Framework.Game

{

    GraphicsDeviceManager graphics;

 

    //**2**

    public GameClass()

    {

        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

 

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

        Components.Add(new GamerServicesComponent(this));

    }

 

 

    //**3**

    protected override void Update(GameTime gameTime)

    {

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

        // Write some data to our storage when we press 'X'

        if (GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed)

        {

            Object obj = (Object)"GetDevice for Player One";

            Guide.BeginShowStorageDeviceSelector(StorageDeviceSelected, obj);

        }

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

        //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

 

        base.Update(gameTime);

    }

 

    //**4**

    //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

    //**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**SAVEFILE**

    private void StorageDeviceSelected(IAsyncResult result)

    {

        // This is our function which gets called when we are allowed to write

        // or read data to the storagedevice file (gives us a valid path)

        StorageDevice device =

        Guide.EndShowStorageDeviceSelector(result);

        StorageContainer container =

        device.OpenContainer("StorageDemo");

 

        StreamWriter writer = new StreamWriter(container.Path + "\\myfile.txt");

        //

        //   On PC this would be in your MyDocuments Folder under your games name

        //   else

        //   On XBOX360 its stored in a place you can only get to through StorageDevice

        //

        writer.WriteLine("lalalalala\n");

        writer.Close();

    }

 

    protected override void Draw(GameTime gameTime)

    {

        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);

    }

}//End GameClass Class

 

 

// Program Entry Point

static class Program

{

    static void Main(string[] args)

    {

        using (GameClass game = new GameClass())

        {

            game.Run();

        }

    }

}

 

 

Of course once you have the path from your callback function, your free to use any of the write methods, such as the BinaryWriter, FileStream, StreamWriter etc etc, all part of the System.IO namespace.

 

 

Some Misc Errors & No-No's on XBOX360
Can't Open a StreamWriter using a direct file name

StreamWriter writer = new StreamWriter("myfile.txt");

writer.WriteLine("somethingtowritetoafile");

writer.Close();

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: UnauthorizedAccessException

 

Can't Open a file using StorageContainer.TitleLocation

string fullpath = Path.Combine(StorageContainer.TitleLocation, "test.txt");

StreamWriter writer = new StreamWriter(fullpath);

writer.WriteLine("lalalalala\n");

writer.Close();

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: UnauthorizedAccessException

 

Can't Save a Texture2D or any inherited Classes using Save(..) Method

Viewport vp = graphics.GraphicsDevice.Viewport;

Texture2D tex = new Texture2D(graphics.GraphicsDevice,

                              vp.Width, vp.Height, 1,

                              TextureUsage.None,

                              SurfaceFormat.Color);

tex.Save("myfile.jpg", ImageFileFormat.Jpg); //Doesn't Exist on 360! (only PC)

error CS0117: 'Microsoft.Xna.Framework.Graphics.Texture2D' does not contain a definition for 'Save'

 

 

 

Always good to log information, letting you save high scores, the players last position or anything else you think the player would need...and they can reload it anytime they want.  Its also good if you maybe want to let the player take screenshots of there game, or if you want to write a log of what's been happening...for example if they have a car game and they customize there car, you'd definitely want to save there custom setup :)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 Visitor: 9534626  { 209.237.238.175 } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.