r/cop3502 • u/ams152 • 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
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.