• Advertisement
Sign in to follow this  

C# Question

This topic is 3876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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~

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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~~

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Here is an article that explains how to clear a console window with C#. There is also another article that describes on clearing the window, but it is unnecessarily difficult in my opinion. I included it just in case you want to see (I have no idea why it is written in the first place).

Mike and others already told with distinction some ways to achieve yours goals. I would, however, add the using statement to the mix. You don't have to understand the concept in full yet, but this is good to know knowledge when you are actively pursuing to write better code. You probably discover later on also that you can serialise -- that is, persist, objects -- and load them with little effort. For instance, Dictionary class might come handy later in your case in saving your game settings (using the aforementioned serialisation).

And before closing this posting, here's a snippet on how to use using with writing files, for instance.

using(StreamWriter writer = File.CreateText("save.txt"))
{
writer.Write("Text");
}



Share this post


Link to post
Share on other sites

You probably have forgotten semicolon from somewhere, you have used wrong reserved words (using, foreach, for etc.) in a wrong place or in a wrong order. These are the most common mistakes.

It could happen that the compiler runs through your code expecting some word to appear it has judged from some previous statement. When it doesn't find what it was looking for, it tells that it was expecting something (at the end of the document).

Share this post


Link to post
Share on other sites
I am using Visual Studio 2005 Express C#, and it gives me no other errors but that one... If i was missing a semicolon, etc. It would tell me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement