Using the same condition to end one item and start another?

This isn’t really a problem of the same “condition” not being able to be used twice, but that you are calling a function that gives a different result each time, which doesn’t really make it suitable to be regarded as a “condition”.

e.g. you call event.getKeys() and detect that a key has been pressed. You then call that function again: it won’t give a result unless a new key press event has occurred.

What you need to do is shift this code out of the conditions field and put it into a code component. In there, you will have code that runs every frame and checks for keypresses. If one of your desired keys is pressed, it sets some variable to True. You then use that variable name in your condition field. That way, it can be used multiple times, as the value of the variable remains constant. e.g. insert a code component and put this in the “begin routine” tab:

number_pressed = False # need an initial value

and then put something like this in the “Each frame” tab:

if event.getKeys(keyList=[‘1’, ‘2’, ‘3’]):
    number_pressed = True

and then put number_pressed in your conditions field. Make sure that the code component is above the other components, so they get access to the latest value of number_pressed on every frame.

Note that here although we are checking event.getKeys() repeatedly, it won’t reset the value of number_pressed to False when a keypress isn’t detected: if there is no keypress, nothing happens.

1 Like