Have code inside if else statement run continuosly

Hi, I’m trying to have different bits of code inside draw() run continuously but be activated by different key presses.

The problem is that what is inside the if else statement only runs while I am pressing the key.

What I’d like is to be able to do, in general, but I’m having trouble implementing it in Processing, is to switch to different “modes”, each of which should continuously once activated, just like a simple draw() function would do.

Any suggestions will be greatly appreciated!

Here is an example of the issue:

def setup():
    size(400,400)
    background(255)
               
class Point(object):
    
    def __init__(self,position):
        self.position=position
        
    def vector(self):
        stroke(0)
        #self.position.mult(0.3)
        line(self.position.x,self.position.y,mouseX,mouseY)
        
        
def draw():
    
    
    if keyPressed==True and key=="a":
        background(255)
        for i in range(0,width,20):
            a=Point(PVector(i,0))
            a.vector()
            b=Point(PVector(i,height))
            b.vector()        
        for j in range(0,height,20):
            c=Point(PVector(0,j))
            c.vector()
            d=Point(PVector(width,j))
            d.vector()
            
    elif keyPressed==True and key=="s":
        background(255)
        for i in range(0,width,40):
            a=Point(PVector(i,0))
            a.vector()
            b=Point(PVector(i,height))
            b.vector()        
        for j in range(0,height,40):
            c=Point(PVector(0,j))
            c.vector()
            d=Point(PVector(width,j))
            d.vector()
1 Like

You should probably have the else function that recursively calls itself, I don’t know python, but here’s a psuedocode interpretation:

keypressed(){
if(key=="f"){
recurse();
}
}
recurse(){
dosomething();
recurse();
}

If I call the function wherein the if statement is contained—draw()—at the end of the if statement, Processing feels I have exceeded the maximum recursive depth an outputs this:

Runtime Error: maximum recursion depth exceeded(Java StackOverflowError)

You could probably make an integer or some other value, indicating what state it is right now, and then use that.

state = 0
...
def draw():
  if keyPressed==True and key=="a":
    state = 1
  elif keyPressed==True and key=="s":
    state = 2

  if state==1:
    #Do stuff
  elif state==2:
    #Do other stuff
1 Like

I tried, but it keeps telling me the local variable “state” was referenced before assignment. I’ve tried assigning “state” in setup() and before setup(), have even asked setup() to print the variable “state” after having assigned it, and it does, but then says it is not assigned!

def setup():
    state=0
    print(state)
    size(400,400)
    background(255)

def draw():
    
    
    if keyPressed==True and key=="a":
        state=1
        
    elif keyPressed==True and key=="s":
        state=2
            
    if state==1:
       _#1st mode_
            
    elif state==2:
        _# 2nd mode_
       
1 Like

Hi,

You need to make your variable global

state = 0

def setup():
    size(400, 400)
    fill('#000000')

def draw():
    background('#ffffff')
        
    text('STATE = %i' % state, width>>1, height>>1)
        
    
def keyPressed():
    global state

    if key == 'a': state = 1
    elif key == 's': state = 2
1 Like

Yes! That’s it, thanks @solub and all.