0

I made a content pipeline extension (using this tutorial) in XNA 4.0 game.

I altered some aspects, so it serves my need better, but the basic idea still applies. Now I want to go a step further and enable my game to be changed during run time. The file I am loading trough my content pipeline extension is very simple, it only contains decimal numbers, so I want to enable the user to change that file at will and reload it while the game is running (without recompiling as I had to do so far). This file is a very simplified version of level editor, meaning that it contains rows like:

1 1,5 1,78 -3,6 

Here, the first number determines the object that will be drawn to the scene, and the other 3 numbers are coordinates where that object will be placed.

So, how can I change the file that contains these numbers so that the game loads it and redraws the scene accordingly?

Thanks

NDraskovic
  • 706
  • 2
  • 22
  • 50

2 Answers2

1

Considering you've created a custom content pipeline extension I presume you know how to load in data using streamreader? Where you could just empty your level data and load new data in by reading through the text file line by line?

The reason I mention this is because as far as I am aware it's not possible to load in data through the content pipeline during runtime especially because the xna redistribute does not contain the content pipeline.

Another option could be to change to using xml for the level file and use XElement which I quite recently found and this is my current method.

Here is a commented example of using StreamReader to load in simple level data from a .txt file. http://pastebin.com/fFXnziKv

Jon
  • 163
  • 7
  • If you could give me a link to any examples I would be grateful. – NDraskovic Apr 05 '12 at 11:55
  • 1
    This is how I used to load in my levels, my uni lecturer taught us this to quick start us on a project. [link](http://pastebin.com/fFXnziKv) I commented the load method as much as I could so hopefully you can understand it. – Jon Apr 06 '12 at 11:30
  • This is a great link, thanks, I'll need some time to implement it and see if it works for me (I think it should because I use a similar way of formating data in my file). Again thanks for your answer – NDraskovic Apr 10 '12 at 07:27
  • Ok just to give you some feedback - I managed to apply the solution from your link, and now I just have to alter it so it fits my needs. Thank you very much, I was trying to solve this problem for almost a month now, you have saved me a lot of time. I will mark your answer as correct one, so if you could edit it to contain the link you provided me, it could help someone else reading this post. Thanks again – NDraskovic Apr 10 '12 at 07:53
0

In XNA 4, if you are using StorageContainer, you can do something like:

(...)
StorageContainer storageContainer = //get your container

Stream stream = storageContainer.OpenFile("Level.txt", FileMode.OpenOrCreate);
StreamReader sr = new StreamReader(stream);

while (!sr.EndOfStream)
{
    String line = sr.ReadLine();
    //use line to do something meaningful
}
stream.Close();
storageContainer.Dispose();
(...)

From personal experience, if you go for raw TextReader, the only problem is to get the path of your Content folder, which can be relatively easy to retrieve (in Windows only!)

Fermin Silva
  • 3,331
  • 2
  • 17
  • 21
  • I'm not using StorageContainer (at least not yet). I'll try the TextReader, but I had some problems when I tried to use the File.ReadAllText method (when I insert the path the compiler throws me an Access Exception). – NDraskovic Apr 10 '12 at 07:31