Skip to content

Instantly share code, notes, and snippets.

@xcvd
Created April 9, 2012 00:06
Show Gist options
  • Save xcvd/2340467 to your computer and use it in GitHub Desktop.
Save xcvd/2340467 to your computer and use it in GitHub Desktop.
Passing function example
/**
* 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