Sign in to follow this  
rherm23

C# file IO

Recommended Posts

I think i might be burned out from working all night and im pretty sure this is a simple question that im waaay overthinking so help here would save me some grief. I am working on a game ive been mulling over for a bit. Here is the problem ....

I want to have a text file say ......

This is a paragraph about monkeys and how much they love
to fling poo. I found this concept to be foo foo foo to the
foo power and blah blah blah.


in my C# i want to read this in.
at some point i want to then break up that string into smaller strings.
The idea is i want to display the text into a custom GUI_LABEL type object
for a text game.

for some reason when i sit to code this out i go blank or just really mess this up.
In c++ this wasnt really an issue for some reason but in C# its rocking my face.

Share this post


Link to post
Share on other sites
So what exactly is your problem? Reading the file in? Splitting the content?

string content = File.ReadAllText(filePath); // reads in the file content as a string
string[] allYourParts = content.Split(yourSplitCharacter); // split by the character in yourSplitCharacter

string[] allFileLines = File.ReadAllLines(filePath); // read every line of the file into a new string into the string array


Did this help?

Share this post


Link to post
Share on other sites
There are a couple approaches -- one would be to not store "just text", but to structure the data using some kind of markup like XML, YAML or even JSON. Another approach would be to parse the data and recognize certain words as being special. The latter is probably less flexible: imagine a lamp that exists in your game, this lamp is special, perhaps you can pick it up, so you want it to display in yellow text. But, not all lamps are special, and we don't want to confuse the user by coloring these useless lamps with any special color...

So, if you go with the latter, you probably want to be a little more robust and impliment a custom mark-up language of sorts, something like:

$location:Darkened Room;
You enter a darkened room. The air hangs thick with a musky odor. $ifnottaken:lamp - A $item:lamp; flickers dimly in the far corner.


Something like that would be relatively easy to parse with a regular expression.

If I were doing a text game, I'd probably do the bulk of it using XML to structure the data, along with a simple scripting engine to handle things that aren't static -- similar to how javascript is used in webpages. You could embed something standard like Lua, or roll your own simple solution. In any event, embed the scripting content inside XML tag bodies or string-based tag attributes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this