Jump to content
  • Advertisement
Sign in to follow this  
mohaakilla51

C# Question

This topic is 3968 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!