www.xbdev.net
xbdev - software development
Thursday December 26, 2024
Home | Contact | Support | XNA & 360 The bits and bytes of XNA and C# for your 360... | XNA & 360 The bits and bytes of XNA and C# for your 360...
>>
     
 

XNA & 360

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

 

 

Its just nuts that you can't just open a file that you've added to your project!  But its just the way its been done.  Instead each time you add a file to your xbox which has some format that it doesnt' know about...which included '.txt', '.bin' or any other format that you want to load yourself...then you have to actually add a

 

You might want to open a model file in game to read some data in or a txt file for some reason for some game data and parse it on the game side and not at compile/build time....this is what we'll show you here!

 

Data files, txt files, model files etc, you can load these manually by adding them as.

 

Quick steps to allowing you to read a file:

 

  • Right click on Solution in explorer and select Add->New Project
  • Content Pipeline Extention from Dialog
  • On the New ContentPipeline Project, click Add->New Item
  • Content Importer form the Add New Item Dialog
  • In your main project you have to add it to the references
  • Right click Reference Add Reference, and select Project Tab at the top Dialog
  • And you should notice your ContentPipeline in there
  • Now Add your file (e.g. .txt file, or .bin file to your Content folder)
  • One final thing, select your .txt file in the solution explorer and click the properties, Copy to Output Dir, No Processing in Content Processor, Content Importer your importer you created.

 

Also incase you get any problems I did some screenshots :)

 

 

Error Message if you've not added a Content Importer:
Error : Cannot autodetect which importer to use for "myfiletoload.txt". There are no importers which handle this file type. Specify the importer that handles this file type in your project.

 

 

Some example source code to show the reading in of the simple txt file...

 

MyContentImporter.cs

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Content.Pipeline;

using Microsoft.Xna.Framework.Content.Pipeline.Graphics;

 

using TImport = System.String;

 

namespace MyContentPipeline

{

    [ContentImporter(".All", DisplayName = "All Importer", DefaultProcessor = "AllProcessor")]

    public class MyContentImporter : ContentImporter<TImport>

    {

        public override TImport Import(string filename, ContentImporterContext context)

        {

            // Doesn't really matter as we won't do any processing

            return "";

        }

    }

}

 

 

Game.cs [Download Full SourceCode]

using System;

using System.Collections.Generic;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio;

using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Net;

using Microsoft.Xna.Framework.Storage;

 

 

public class Game : Microsoft.Xna.Framework.Game

{

    GraphicsDeviceManager graphics;

    public Game()

    {

        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

    }

 

    protected override void LoadContent()

    {

        //**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**

        //**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**

        // Our Content is ready to be used here so we'll print out all the files

        // that are with our file

        System.IO.DirectoryInfo dr = new System.IO.DirectoryInfo(StorageContainer.TitleLocation);

        if (dr.Exists)

        {

            foreach (System.IO.DirectoryInfo dir in dr.GetDirectories())

            {

                System.Diagnostics.Debug.WriteLine("[" + dir + "]");

 

                foreach (System.IO.FileInfo fl in dir.GetFiles("*.*"))

                {

                    System.Diagnostics.Debug.WriteLine(fl);

                }

            }

        }

 

        // Just for that extra check, we'll actually read our txt file in

        // and write its contents out to the debug output window

        System.IO.StreamReader data = new System.IO.StreamReader(StorageContainer.TitleLocation + "\\Content\\" + "myfiletoload.txt");

        string str = data.ReadLine();

        System.Diagnostics.Debug.WriteLine(str);

 

        //**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**

        //**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**FILE**

    }

 

    protected override void Update(GameTime gameTime)

    {

        // Allows the game to exit

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

            this.Exit();

        base.Update(gameTime);

    }

 

    protected override void Draw(GameTime gameTime)

    {

        graphics.GraphicsDevice.Clear(Color.BurlyWood);

        base.Draw(gameTime);

    }

}

 

// Program Entry Point

static class Program

{

    static void Main(string[] args)

    {

        using (Game game = new Game())

        {

            game.Run();

        }

    }

}

 

 

The first few lines are the folders and the files...I places the file 'myfiletoload.txt' in the Content folder, which is why it shows up as in a sub directory.  And of course the last line is the first line of the contents of the txt file.

 

Debug Output Window:
[Content]
myfiletoload.xnb
myfiletoload.txt
this is my file on the xbox?

 

 

Hopefully its been interesting and has given you some insight into files even if you decide to do it another way.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.