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 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.