<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Musings of a Game Developer &#187; XNA Programming</title>
	<atom:link href="http://www.johnwordsworth.com/category/xna-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnwordsworth.com</link>
	<description>John Wordsworth&#039;s musings on Game Programming &#38; Computer Design</description>
	<lastBuildDate>Wed, 18 Aug 2010 20:19:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>XNA 3.0 &#8211; Reading Text Files on the Xbox</title>
		<link>http://www.johnwordsworth.com/2009/01/xna-30-reading-text-files-on-the-xbox/</link>
		<comments>http://www.johnwordsworth.com/2009/01/xna-30-reading-text-files-on-the-xbox/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 12:42:35 +0000</pubDate>
		<dc:creator>John Wordsworth</dc:creator>
				<category><![CDATA[XNA Programming]]></category>
		<category><![CDATA[Reading Text Files]]></category>
		<category><![CDATA[Xbox Community Games]]></category>
		<category><![CDATA[Xbox XNA]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.johnwordsworth.com/blog/?p=28</guid>
		<description><![CDATA[We are making strong progress on an XNA Community Game title that we are working on and I have just spend a good 30 minutes trying to figure this out. Hence, I'm writing this as a future reference for myself and in the hope that it might help any coders out there trying to achieve the same thing.]]></description>
			<content:encoded><![CDATA[<p>We are making strong progress on an XNA Community Game title that we are working on and I have just spend a good 30 minutes trying to figure this out. Hence, I&#8217;m writing this as a future reference for myself and in the hope that it might help any coders out there trying to achieve the same thing.</p>
<p><strong>The Problem</strong></p>
<p>Our game is relatively simple and we would like to define the stages that the player progresses through in a plain text file. While it&#8217;s a very simple task to read a text file in C#, I initially had problems assuming that loading a new StreamReader on the the Xbox like so <strong>StreamReader(&#8220;LevelIndex.txt&#8221;);</strong> would load up a text file in the same directory as my executable. Not the case &#8211; it turns out that, as the Xbox doesn&#8217;t really follow a directory structure like a PC, this doesn&#8217;t work.</p>
<p><strong>The Solution</strong></p>
<p>The solution is also relatively simple, but not east to find &#8211; as it&#8217;s one of those things that you either know how to do or not. There are 2 important things to note before jumping directly into a code listing.</p>
<p><strong>1. StorageContainer.TitleLocation:</strong> In order to construct the file path that you will be feeding into a StreamReader, you must prefix your path with this property. To load up the file &#8216;LevelIndex.txt&#8217;, which resides in the root directory of your solution, you must construct the path as follows; <strong>String fullPath = StorageContainer.TitleLocation + &#8220;LevelIndex.txt&#8221;;</strong></p>
<p><strong>2. Build Action (None), Copy to Output Directory (Copy if Newer):</strong> As your text file is not going to be processed by the Content Processor, you need to tell Visual Studio what should be done with your text file. For my project, I dragged and dropped the text file into my Solution (but NOT in the Content directory). The text file sits alongside your .cs files (although, could reside in a folder). Then, you must change the properties of the text so that Build Action is set to &#8216;None&#8217; and Copy to Output Directory is set to &#8216;Copy if Newer&#8217;. This ensures that Visual Studio doesn&#8217;t try to compile the file as code or include it in your Content repository. It will simply copy the text file to the TitleLocation of your game &#8211; just what you want so that you can read it from within your title.</p>
<p><strong>Code Listing</strong></p>
<p>The following code listing shows how we have implemented a simple text file reader for an Xbox XNA Project;</p>
<pre>function String[] readStageTitles( String filePath ) {
  ArrayList stageTitles = new ArrayList();
  String[] returnData = null;

  // Both Windows and Xbox - Ensures we are looking in the game's folder.
  String StageIndexPath = StorageContainer.TitleLocation + "\\" + file;

  try
  {
    StreamReader streamReader = new StreamReader(StageIndexPath);
    String line;

    while ((line = streamReader.ReadLine()) != null)
    {
      String[] data = line.Split(';');

      if ( data.Length == 2 ) {
        stageTitles.Add(data[1]);
      }
    }

    streamReader.Close();
  }
  catch( Exception ex )
  {
    // Do things here incase it can't read the file
  }

  returnData = (String[])stageTitles.ToArray( typeof(String) );
  return returnData;
}</pre>
<p>Hope this helps someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnwordsworth.com/2009/01/xna-30-reading-text-files-on-the-xbox/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
