Waiting until a condition is true

edited May 2016 in Python Mode

I’m trying to make a function that will pause the program until a condition is true. Here’s what I thought would work:

def wait_until( x ):
    while not x():
        pass

I’ve tried it with something like wait_until( lambda: keyPressed ) or wait_until( lambda: mouseX > 250 ) but neither of those works — the functions I input never seem to return True. Can you help me figure out why?

I don’t think it’s a problem with wait_until — I’ve found that it doesn’t work even if I just say something like this:

def setup():
    size( 200, 200 )
    while not keyPressed:
        pass
    background( 255 )

Answers

  • edited May 2016 Answer ✓
    # forum.processing.org/two/discussion/16450/
    # waiting-until-a-condition-is-true
    
    # GoToLoop 2016-May-05
    
    paused = False
    
    def setup():
        size(800, 600)
        frameRate(1)
    
    
    def draw():
        background(int(random(0x1000000)))
        frame.setTitle('Frame: ' + `frameCount`)
    
    
    def keyPressed():
        global paused
        k = chr(keyCode) if key != CODED else keyCode
    
        if k == 'P':
            paused ^= True
            noLoop() if paused else loop()
    
        paused and frame.setTitle('Sketch is paused...')
    
Sign In or Register to comment.