C# Question

Started by
12 comments, last by mohaakilla51 16 years, 9 months ago
I am creating a very simpe text based RPG in the Console, how would I go about the Save/Load function? Would I create files (obviously). How do I do this? Can someone point me to a good free C# book? I thought about joining the C# Workshop,but that really is not how I learn best, answering questions is really quite easy for me, and I learn nothing from it. I also tried reading the files that the workshop told me to download, but the tone they are written in is too clinical and I find myself skimming and not actually reading and retaining information. So: First thing I would like to know is: how do I create files for save game/ load game? also: -are there any GOOD free resources out there for C#?(besides the files from the workshop)? -Is there a better way to get user input than Console.ReadLine()? Any help is always appreciated, ~MOHAAkilla516~
Advertisement
Quote:Original post by mohaakilla51
First thing I would like to know is:
how do I create files for save game/ load game?

You should take a look at the File and FileStream classes in the System.IO namespace. Also, there are the StreamReader and StreamWriter classes, which make dealing with text easy. For example:
public void Save(string fileName){        // open the file    StreamWriter writer = new StreamWriter(fileName, false);    // write your stuff here    writer.Write("Blah");        // close the stream    writer.Close();}public void Load(string fileName){    // open the file    StreamReader reader = new StreamRead(fileName);    // read your stuff here    reader.Read();    // close the stream    reader.Close();}


Quote:
also:
-are there any GOOD free resources out there for C#?(besides the files from the workshop)?

There aren't any GOOD and FREE resources out there except for the standard. If you want something more, consider picking up a real book. However, you can and should use MSDN, Microsoft's reference site.

Quote:
-Is there a better way to get user input than Console.ReadLine()?

For a console application, not really. What does it lack?
Mike Popoloski | Journal | SlimDX
I appreciate you are learning but I would add that it might be worth googling reflection for saving and loading!

With this you can save game data directly!
Reflection is not what you are looking for. What you are looking for is Serialization. That will allow you to just save the current state of all of your classes.

theTroll
Thank you all for your help, I will look up that Serialization.

And my problem with Console.ReadLine() is that it isn't a very efficient way of getting user input from menus. For example, lets say I make a main menu that looks like this:

------------
Game name
------------
Start New Game
Load Saved Game
Options
Exit

The only way to get the users choice is to ask him which option like this:
Console.Write("What would you like to do? ");choice = Console.ReadLine();Switch(choice){  case "blah blah":     blah blah;     break;}
which in my opinion is not very user friendly. I know I should go to a GUI if I want user friendly menus, but that really is not wat I want to do right now, as I want to start with the basics and work my way up. :|
So, for a console app, is there any way to get user menu choice by using the up,down, enter keys or something?
You might want to poke around here.

Console.KeyAvailable will tell you if a key is available for reading.
You can also use Console.ReadKey() to block for a keypress.
You could try to clear the console buffer and reprint the menu with a '*' next to the current line every time a user hits an up/down arrow. If this would even work it seems rather hackish.

I had the same problem a long time ago when trying to have an animated cursor that seemed like it was spinning. I basically just wrote a backspace character to the buffer to delete the current cursor and replace it with the next 'animation'. Although this was in C.
Thank you both, that is exactly what I was looking for. I know I am full of questions, but this should be my last one for a while: How do I clear the console screen? I googled it and got this craplong explanation and like 20 lines of code. Is there no easier way to do this?

Thanks Again,
~~MOHAAkilla516~~
Calling Console.Clear() should suffice.
Here's an msdn example of using Console.Clear()
Sorry, I asked the wrong question... :(
what I meant to ask was I got this error everytime i try to run:
type or namespace expected, or end-of-file expected. It is the last line in the code, what do I need for it to be an end of file?

This topic is closed to new replies.

Advertisement