Jared Price
Jared Price

Reputation: 5375

C# Console App, skipping lines of code from a method to a line in the main method

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
}

Upvotes: 0

Views: 1019

Answers (1)

Coding Flow
Coding Flow

Reputation: 21881

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

e.g.

static void Main()
{
    LoadGame();
}

Upvotes: 1

Related Questions