-2

I'm pretty new to C# and coding in general so it is hard for me to explain and this might be something simple.

The program I am trying to make changes values in the game (Assault Cube).

Without the inner while loops, it just changes the values, I would like them to loop.

I have one outer while loop with multiple loops inside. The inner while loops are there to loop the function but it stops the outer while loop. I would like multiple inner loops to run along with the outer one.

I have tried what feels like everything. I tried Booleans, Breaks, Returns. But nothing I have tried has fixed my problem. It may not be because they don't work, it may just be me using them wrong.

        while (true) //outer loop
        {

            Console.Title = "SlavScripts";

            int address1 = LocalPlayer + Health;

            int address2 = LocalPlayer + Armor;

            string Player = "";

            Console.WriteLine("---SlavScripts v2.0---");
            Console.WriteLine("");
            Console.WriteLine("[1] Player Options");
            Console.WriteLine("");

            string answer = "";
            answer = Console.ReadLine();


            if (answer == "1")
            {
                Console.WriteLine("--Player Options--");
                Console.WriteLine("");
                Console.WriteLine("[1] Godmode");
                Console.WriteLine("[2] Armor");
                Console.WriteLine("");
                Player = Console.ReadLine();

                if (Player == "1")
                {
                    Console.WriteLine("Godmode Enabled");
                    Console.WriteLine("");
                    while (true)
                    {
                        vam.WriteInt32((IntPtr)address1, 6969); //value to loop.
                    }
                }
                else if (Player == "2")
                {
                    Console.WriteLine("Infinite Armor Enabled");
                    Console.WriteLine("");
                    while (true)
                    {
                        vam.WriteInt32((IntPtr)address2, 6969); //value to loop.
                    }
                }
            }
        }

(full code: https://pastebin.com/bBcBPYs6)

Expected:

  1. I enter the corresponding digit to activate the function
  2. The function that was activated loops, and original text appears which allows me to navigate to another function.

Actual:

  1. I enter the corresponding digit to activate the function.
  2. The function activates and loops, but does not show opening text and disallows my to type further.
A. Nadjar
  • 2,440
  • 2
  • 19
  • 20

3 Answers3

1

Think about what is happening in your code. Each instruction in your code is executing one after the other (superficially thinking - this might not be exactly true at assembly execution level but you get the idea). For example the line if (answer == "1") will only executed when the line before it (that is answer = Console.ReadLine();) completes its execution.

When you create the "outer while loop" (as you called), everything inside the loop will execute 1 instruction at a time following the order they are written and when the last instruction inside the loop is executed, the execution will jump back to the first line of code inside the loop.

If you put another while loop inside the "outer one", and say it will not have an exit condition (by declaring while(true) you are creating a loop that will never stop executing its embedded statements). Therefore when the execution reaches one of these while(true) loops it will be trapped inside them looping its embedded statements forever.

What I described is how instructions are executed. The other part you need to know is what is a thread. You can think of a thread as an isolated machine where every code is executed. When you don't deal with threads directly in your code, the compiler will automatically asks the operating system to create a thread to run your code at - this is usually referred to as the main thread.

So, what you actually need in your code is to inform the operating system to put the execution of each one of those "inner while(true)" loops inside a thread other than the main one and leaving the main thread to execute only code that can sequentially be executed.

You can learn how to use threads in your application here.

And as a side-note: that is probably not what you want to create a loop spinning code every cycle. You should consider pausing the execution inside those "inner loops" by putting the thread that is executing it to sleep for some time each time it iterates through (you can do this by just calling System.Threading.Thread.Sleep(100) - it will pause the thread execution by 100 milliseconds and will save some cpu execution time).

andresantacruz
  • 1,676
  • 10
  • 17
0

As per your expectation, Please find the sample code snippet for getting user input continuously - achieved using Task in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

 namespace SampleTaskStop
{

class Program
{
    static public void SetValue(string address, int value)
    {
        while (!_cancelled)
        {
            //vam.WriteInt32((IntPtr)address1, 6969); //value to loop.
        }
        Console.WriteLine(address + " - completed");
    }

    static bool _cancelled = false;
    static void Main(string[] args)
    {
        while (true) //outer loop
        {
            Console.Title = "SlavScripts";

            Console.WriteLine("---SlavScripts v2.0---");
            Console.WriteLine("");
            Console.WriteLine("[1] Player Options");
            Console.WriteLine("");

            string answer = "";
            answer = Console.ReadLine();
            _cancelled = false;

            if (answer == "1")
            {
                var acceptUserInput = Task.Factory.StartNew(AcceptUserInput);
                acceptUserInput.Wait();
            }
        }
    }

    private static void AcceptUserInput()
    {
        // Task to perform the value setting in the
        Task computationTask = null;
        Console.WriteLine("Enter Player Input");
        Console.WriteLine("1 God Mode");
        Console.WriteLine("2 Armour Mode");
        Console.WriteLine("Press Esc to cancel");

        var key = Console.ReadKey(true);

        while (key.Key != ConsoleKey.Escape)
        {
            if (key.Key == ConsoleKey.D1 || key.Key == ConsoleKey.NumPad1 )
            {
                Console.WriteLine("Godmode Enabled");
                Console.WriteLine("");

                _cancelled = true;
                if (computationTask != null)
                {
                    computationTask.Wait(new System.Threading.CancellationToken());
                }
                _cancelled = false;

                computationTask = Task.Factory.StartNew(() => SetValue("data1", 6979));
            }
            else if (key.Key == ConsoleKey.D2 || key.Key == ConsoleKey.NumPad2)
            {
                Console.WriteLine("Infinite Armor Enabled");
                Console.WriteLine("");
                _cancelled = true;

                if (computationTask != null)
                {
                    computationTask.Wait(new System.Threading.CancellationToken());
                }
                _cancelled = false;

                computationTask = Task.Factory.StartNew(() => SetValue("data2", 6979));
            }

            key = Console.ReadKey(true);
        }
        _cancelled = true;
        Console.Write("Computation was cancelled");
    }
}

}

Nareen Babu
  • 443
  • 1
  • 5
  • 13
0

Thanks to everyone who replied!

Multi-Threading sounds alot harder than it actually is and it also simplified my code alot!

Still have to add Thread.Sleep's :D

Cheers!

New & Improved - pastebin.com/qN7ci0By