shook_one
shook_one

Reputation: 347

Console.ReadLine() only works once despite it being in a while loop

To give some context for the code, I'm modifying the game "AssaultCube".

So this is a console program. When it launches, you can type stuff in and if you type in "1", it'll start setting the health value to 999 in a loop. However, you can't type more stuff in because the loop isn't over, but in order to end the loop, I need to be able to type "1" to toggle it off. I want to be able to toggle this on and off each time I type in "1". It seems like a simple problem and I've been trying to get this to work for hours with no luck and my brain is fried. Thanks in advance and sorry if I was unclear in my explanation, I'm not good at those :D.

while (true)
        {
            string Select;
            Select = Console.ReadLine();

            if (Select == "1") //If the number "1" is typed, do stuff
            {
                int finalHealth = localPLayer + health; //Add the Base and Health addresses together

                if (healthToggle == false)
                {
                    healthToggle = true;
                    Console.WriteLine("\n[1] Unlimited Health activated\n");

                    while (healthToggle) //While Health Toggle is TRUE, do stuff
                    {
                        vam.WriteInt32((IntPtr)finalHealth, 999); //Set finalHealth to 999 in a loop, making you invincible
                        Thread.Sleep(100); //Let CPU rest
                    }
                }
                else
                {
                    healthToggle = false;
                    Console.WriteLine("\n[1] Unlimited Health deactivated\n");

                    vam.WriteInt32((IntPtr)finalHealth, 100); //Set health value back to normal
                }
            }
            Thread.Sleep(100);
        }

Upvotes: 0

Views: 372

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

I agree with 41686d6564, Console.KeyAvailable and Console.ReadKey() are definitely the way to go.

Try this out...

static void Main(string[] args)
{
    bool quit = false;
    while (!quit)
    {
        Console.WriteLine("Press Esc to quit, or 1 to start/stop.");
        while (!Console.KeyAvailable)
        {
            System.Threading.Thread.Sleep(100);
        }
        ConsoleKeyInfo cki = Console.ReadKey(true);
        if (cki.Key == ConsoleKey.Escape)
        {
            quit = true;
        }
        else if (cki.Key == ConsoleKey.D1)
        {
            Console.WriteLine("\n[1] Unlimited Health activated\n");
            bool godMode = true;
            while (godMode)
            {
                // ... do something ...
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") + ": ...something ...");

                System.Threading.Thread.Sleep(100);
                if (Console.KeyAvailable)
                {
                    cki = Console.ReadKey(true);
                    if (cki.Key == ConsoleKey.D1)
                    {
                        godMode = false;
                    }                            
                }
            }
            Console.WriteLine("\n[1] Unlimited Health deactivated\n");
        }
    }
    Console.WriteLine("Goodbye!");
    Console.Write("Press Enter to Quit");
    Console.ReadLine();
}

Upvotes: 1

Related Questions