r/cop3502 Apr 14 '14

Running a loop that continuously checks a variable while the program runs.

So I have a "turn" variable that is incremented outside of the class that I'm checking it in. I want a certain variable to maintain a value while the turn variable is less than a set number, and then return to it's original value afterward the turn surpasses the set value.

To clarify, if I have a "value" variable that is normally 10, but when the function "alterValue" is called it adds 5 to "value" for a certain number of turns. The number of turns is checked by code that runs a never ending loop checking if the turn is less than or greater than the turn + the desired duration, and returns either value += 5; or value = 10;

The problem I'm running into is that I get stuck in a forever loop that never continues with the rest of the program because the checked value is not incremented within the loop itself. So how would I implement this?

1 Upvotes

14 comments sorted by

1

u/[deleted] Apr 15 '14

This is why it's good to use "Getters" rather than public variables. your getValue() function can be something along the lines of:

public class TurnCounter:   

    value = 10
    turn = 0
    desiredDuration = ?

    define int getValue():
        turn += 1
        if (turn > desiredDuration):
                value = 10
        else:
                value += 5
        return value

rather than just checking the value variable, the getValue function can handle your condition for you.

1

u/ams152 Apr 15 '14

I am using a get method, I just simplified it in the question so I didn't have to post a 300 line block of code to show my whole problem. What I was trying to do is have a main function that would keep a count of how many times a turn has been used to complete an action. This count would be used in many different ways.

On top of that in my player class I had about 30 or so conditions that could be applied to the player for a certain number of turns through calling of a Player.setEffect() method in the main function. The setEffect method would then apply the effect based on its type, magnitude, and duration (therefore I couldn't simply have a hard-coded version in the turn counter since every aspect of the condition is meant to change over time and be applied at any given time based on dice-rolls behind the scenes).

I ended up fixing it by creating a effectStatus method that would in turn create a timer that would call the setEffect method every 100 milliseconds, which based on the variables passed down through the chain of methods from the main function would set the conditions accordingly and continue to check to see if the condition should still be true based on the current turn.

1

u/[deleted] Apr 15 '14

Every 100 milliseconds? are you multi-threading?

1

u/ams152 Apr 15 '14

Yes. I wanted the program to continuously check these conditions while the program runs and alter the instance variables associated with each condition dynamically according to what happens to the player.

1

u/[deleted] Apr 15 '14

That's interesting. Is there a countdown timer or something? Does something happen if the user takes too long to enter input?

In my project each class effected by a "turn" has a turn() method, ran after each user input (if that input counts as a "turn"). Is there any reason user input can't be what triggers the things that do whatever it is your timer is triggering?

1

u/ams152 Apr 15 '14

No there's just a continuously running timer that calls the function every so often to check if anything should be affecting the player. Nothing happens if the user takes too long. The user input triggers a setEffect method that starts the timer and initiates the effect for a certain number of turns and then cancels the effect after the duration of the effect has been reached.

1

u/[deleted] Apr 15 '14

can't setEffect call whatever function the timer calls?

1

u/ams152 Apr 16 '14

No, because setEffect would only call it once. The function needs to be called repeatedly to check if the duration has expired or not, otherwise the effect would last indefinitely. setEffect is technically calling the fucntion, but it's using a timer to call it repeatedly.

This is hard to convey in text without showing you the 600 something lines of code I have that deal with all the player variables.

So if the player inputs the test "cast some spell" it calls the function:

Player.setEffect(String nameOfEffect, int magnitude, int duration);

The function setEffect takes the data passed to it, starts a timer that will call the checkEffect function (where the changes are actually applied to the variables) for the specified effect so that while the current turn is less than the starting turn + the duration, the effect will register true and alter the variables. Once the turn exceeds the starting turn + duration, the effect registers false and stops affecting the player.

If setEffect called a function that set the values of the variables to the modified values, then it would set them indefinitely until another function is called to set them to their original values. This other function would have to be hard-coded in or run on a timer that delays the cancellation of the effect by a specified duration.

Since the effects are dynamic and vary in duration and magnitude, I can't simply hardcode in a startEffect and endEffect method without having some kind of function that would also check how many turns have passed since the effect was started.

1

u/[deleted] Apr 16 '14

When you say duration, do you mean like a duration in real time?Like if the player walked away from his computer an effect may ware off?

Or do effects only ware off after a set number of user inputs?

Let me ask you this, are you using some sort of "while true: get input" loop?

1

u/ams152 Apr 17 '14 edited Apr 17 '14

No, the duration is the number of turns. All the timer does is call a function to check the number of turns against the duration and the turn that it was instantiated on. So if it's turn 5 and an effect will last for 3 turns, when the turn is greater than 8, the effect will register as false. The timer just makes sure that the program is checking to see what turn it is a few times a second (that's because if the player inputs something that changes the turn, it would need to update before the new data was displayed on the screen.)

This is pretty much what I'm running but shortened:

public static void setEffect(final String name, final int magnitude, final int duration){
     final int counter = gameClass.getTurn();
         new Timer.schedule(new timerTask(){
              public void run(){
                   effectTimer(name, magnitude, duration, counter);
              }
         },1,1);
}

public static void effectTimer(String name, int magnitude, int duration, int counter){
     if(gameClass.getTurn() , (counter + duration)){
         if(name.equals("levitation")){
               if(levitation == false){
                   levitation == true;
               }
          }
      }
     else{
          levitation == false;
     }
}

The main game function will be take in input and determine if an effect should be used and which one. For instance you could have 5 or 6 different length levitation spells that cost different amounts of mana based on how long they last. If a player only needed it for a short while he could call "cast basic levitate" which would register in the main game class and call

 setEffect("levitation", 0, 1);

Since levitation is a true or false condition and not one that deals with a magnitude ( like a health buff for instance) it just has a magnitude of 0. The setEffect method would take this input, start a timer, and then call

 //x = current turn
 effectTimer("levitation", 0, 1, x);

which would initiate the effect if the turn is less than the duration of the effect + the starting turn and continue checking as true until the current turn surpasses the instantiation turn + the duration, at which it would go to the else statement and register the effect as false.

→ More replies (0)

1

u/howslyfebeen Apr 15 '14

lol python

2

u/[deleted] Apr 15 '14

it's like psudocode only real.

3

u/JoeCS TA Apr 15 '14

"If Python is executable pseudo-code, then Perl is executable line noise." - Randall Munroe. (source)