views:

1313

answers:

3

I don't really understand how Content importer/processor works in XNA.

I need to read a text file (Content/levels/level1.txt) of the form:

x x
x x
x x

where x's are just integers, into an int[,] array.

Any tips on writting a SIMPLE .txt importer??? By searching google/msdn I only found .x/.fbx file importer examples. And they seem too complicated.

+2  A: 

There doesn't seem to be a lot of info out there, but this blog post does indicate how you can load .txt files through code using XNA.

Hopefully this can help you get the file into memory, from there it should be straightforward to parse it in any way you like.

XNA 3.0 - Reading Text Files on the Xbox

Brett Bender
Why can't I put it into content folder? I put it in there and selected "Copy if newer" and can load it fine with "Game.Content.RootDirectory" + "levels/level1.txt" path?
drozzy
Thanks, this works! Chad wrote out the answer so I'll give it to him
drozzy
+2  A: 

http://www.ziggyware.com/readarticle.php?article_id=69 is probably a good place to start. It covers creating a basic content processor.

Michael
+3  A: 

Do you actually need to process the text file? If not, then you can probably skip most of the content pipeline.

Something like...

string filename = "Content/TextFiles/sometext.txt";
string path = Path.Combine(StorageContainer.TitleLocation, filename);

string lineOfText;
StreamReader sr = new StreamReader(path);
while ((lineOfText = sr.ReadLine()) != null)
{
  // do something
}

Also, be sure to set the "Build Action" to "None" and the "Copy to Output Directory" to "Copy if newer" on the text files you've added. This tells the content pipeline not to compile the text file but rather copy it to the output directory for use as is.

I got this (more or less) from the RacingGame sample provided by Microsoft. It foregoes much of the content pipeline and simply loads and processes text files (XML) for much of its level data.

Chad Meyers