0

new C#er here. I'm working on a console RPG. I've created a save and load method that writes variables to a txt file and loads them when you type "save game" or "load game" at a certain point. I'm trying to get the LoadGame() method to jump to a point in my Main(). I realize I could rewrite some of my code and use some if statements, or goto and just do away with my LoadGame(), but I would prefer to keep my LoadGame() outside of my Main().

Here is basically what my code looks like:

    public static void LoadGame()

    {
        TextReader tr = new StreamReader("SavedGame.txt");

        string charnameload = tr.ReadLine();
        string hpload = tr.ReadLine();
        string hpmaxload = tr.ReadLine();
        string charattackload = tr.ReadLine();
        string charmanaload = tr.ReadLine();
        string charmanamaxload = tr.ReadLine();
        string charmanapowerload = tr.ReadLine();
        string spellsknownload = tr.ReadLine();
        string holyblastknownload = tr.ReadLine();
        string greaterhealknownload = tr.ReadLine();
        string goldload = tr.ReadLine();
        string expload = tr.ReadLine();
        string levelload = tr.ReadLine();
        string inventoryWeaponload = tr.ReadLine();
        string inventoryArmorload = tr.ReadLine();
        string inventoryshieldload = tr.ReadLine();
        string inventoryPotion1load = tr.ReadLine();
        string inventoryPotion2load = tr.ReadLine();
        string inventoryPotion3load = tr.ReadLine();
        string inventoryKey1load = tr.ReadLine();
        string inventoryKey2load = tr.ReadLine();
        string inventoryKey3load = tr.ReadLine();
        string inventoryKey4load = tr.ReadLine();
        string inventoryKey5load = tr.ReadLine();

        characterName = Convert.ToString(charnameload);
        hp = Convert.ToInt32(hpload);
        hpmax = Convert.ToInt32(hpmaxload);
        charattack = Convert.ToInt32(charattackload);
        charmana = Convert.ToInt32(charmanaload);
        charmanamax = Convert.ToInt32(charmanamaxload);
        charmanapower = Convert.ToInt32(charmanapowerload);
        spellsknown = Convert.ToInt32(spellsknownload);
        holyblastknown = Convert.ToInt32(holyblastknownload);
        greaterhealknown = Convert.ToInt32(greaterhealknownload);
        gold = Convert.ToInt32(goldload);
        exp = Convert.ToInt32(expload);
        level = Convert.ToInt32(levelload);
        inventoryWeapon = Convert.ToString(inventoryWeaponload);
        inventoryArmor = Convert.ToString(inventoryArmorload);
        inventoryshield = Convert.ToString(inventoryshieldload);
        inventoryPotion1 = Convert.ToString(inventoryPotion1load);
        inventoryPotion2 = Convert.ToString(inventoryPotion2load);
        inventoryPotion3 = Convert.ToString(inventoryPotion3load);
        inventoryKey1 = Convert.ToString(inventoryKey1load);
        inventoryKey2 = Convert.ToString(inventoryKey2load);
        inventoryKey3 = Convert.ToString(inventoryKey3load);
        inventoryKey4 = Convert.ToString(inventoryKey4load);
        inventoryKey5 = Convert.ToString(inventoryKey5load);

        tr.Close();
        CheckInventory();
        CheckSpell();

    //need statement here to skip to a point in Main()

    }

static void Main()
{
   //skip to somewhere in here
}
Max
  • 12,622
  • 16
  • 73
  • 101
Jared Price
  • 5,217
  • 7
  • 44
  • 74

1 Answers1

1

You are thinking back to front. You call the method in main, where you want it to execute

e.g.

static void Main()
{
    LoadGame();
}
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
  • Yes I understand that. That's not the issue. There are a bunch of things that need to be skipped when the LoadGame() method happens. Perhaps I did not explain my problem well enough. I'm already calling the LoadGame() method within the Main(). I have a switch case for command = Console.ReadLine(); where case ("load game") does the LoadGame() method. I need the method to skip to a line of code in the Main(). – Jared Price Mar 08 '13 at 13:07
  • 1
    Split your LoadGame or other code into multiple methods that carry out the specific task you require then call or not call them as you need. You can then return values from your methods to decide what to do. – Ben Robinson Mar 08 '13 at 13:09
  • I figured it out. I just needed to throw in some if statements. I didn't need to add anything to my LoadGame() method. Thanks for the help tho! – Jared Price Mar 08 '13 at 13:21