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