Created
April 9, 2012 00:06
-
-
Save xcvd/2340467 to your computer and use it in GitHub Desktop.
Passing function example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Waits while a condition is true, with a specified timeout. | |
* | |
* @param condition the method to be evaluated in order to discover if the condition | |
* still holds true | |
* @param timeout the time in milliseconds until the wait times out | |
* @return whether the true if the condition was falisified within the time limit, | |
* otherwise returns false. | |
*/ | |
def waitWhile(condition: () => Boolean, timeout: Int): Boolean = { | |
for(i <- 1 to timeout/50) if (!condition()) return true else Time.sleep(50) | |
false | |
} | |
/** | |
* Waits until a condition is true, with a specified timeout. | |
* This method demonstrates recursion. | |
* | |
* @param condition the method to be evaluated in order to discover if the condition | |
* is true | |
* @param timeout the time in milliseconds until the wait times out | |
* @return whether the true if the condition was falisified within the time limit, | |
* otherwise returns false. | |
*/ | |
def waitUntil(condition: () => Boolean, timeout: Int): Boolean = { | |
if (condition()) return true else Time.sleep(50) | |
if (timeout == 0) { return false } | |
waitUntil(condition, timeout - 50) | |
} | |
/****EXAMPLES OF USE:****/ | |
waitWhile(() => Players.getLocal.getPlane == 0, 4000) //waits while the current plane is 0 with a timeout of 4 seconds | |
waitUntil(selectComplexityConfirm.isOnScreen, 2500) //wait until an interface appears on screen with a timeout of 2.5 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment