mercredi 20 juillet 2016

C# - locking if statements - moving variable

I have a simple script with one moving variable and a few ranges. The variable either increases or decreases at pseudo-random and I have no control over it. The ranges are my if statements.

Each if statement has two commands and I need one of the two commands to be met and executed, before the code can move on.

The problem is the variable can move into a different range before one of the commands is executed, which forces the script to expect two different commands.

How can I keep the script from doing anything else until one of the commands is executed, despite where the variable goes?

Note: It is guaranteed that one of the commands will execute given enough time.

While loops have not worked as they are seen as infinite loops. Bools such as "trigger one or two was executed == true/false" have also not worked, and trapping the code in a bool statement yields the same result as the while loop. I have also tried a switch statement, but it was no different that the collection of ifs below.

In a way, you could say these if statements overlap.

I have looked into using state machine and recursive methods, but at this time they are a little beyond me and don't know if they will work for me now.

The below code is a generic example:

    int MP = moving variable; //updated every iteration of the code
    int R3, R2, R1, S1, S2, S3; //static variables input by user- in descending order
    //MP usually starts between S1 and R1

    if(R3 < MP < R2)
    {
       command one; //an if statement that gives command when triggered
       command two; //another if statement
    }
    if(R2 < MP < R1)
    {
       command one;
       command two;
    }
    if(R1 < MP < S1)
    {
       command one;
       command two;
    }
    if(S1 < MP < S2)
    {
       command one;
       command two;
    }
    if(S2 < MP < S3)
    {
       command one;
       command two;
    }

If it helps, I can bring in the actual code, but I do believe I have narrowed down my problem to this and am portraying it as simple as I know how. I can also go into more detail about any part of this.

I am hoping this is as simple as me overlooking an option, or perhaps there is a something I have not learned about yet.

Thank you for your time

Aucun commentaire:

Enregistrer un commentaire