Replacing the Count CHOP with python?

Hello,

I am familiar with the basics of loops and iterations in Python but when I try to create code to count the keyboard inputs in TD, then increment the value of the variable ‘count’ by +1 everytime the keyboard input is pressed. My code is attached below, everytime I hit the letter ‘e’, it just output all 0-10 values at once.

drum_Counter.15.toe (4.6 KB)

If I understand correctly, you want to count the number of times the ‘e’ key is pressed, right? To do that you’d need a persistent count variable. You could use OP storage to achieve that: OP Class - Derivative

I updated the keyboard DAT callback in your example to use OP storage:

drum_Counter.toe (4.7 KB)

Hello,

Yes this exact what I am trying to do, to count the number of times the keyboard key ‘e’ is hit? Can you explain why exactly i need this OP storage? Also, if i need to retrieve this variable and send it to another operator channel, can that be done?

Thanks!

OP storage is needed to store the count globally instead of locally. There are other ways to do this but this is the most straightforward.

As long as you can access the OP object of the keyboard in DAT you can fetch the storage value using python, so in python scripts, parameter expressions, etc. For example, you could have a channel in the Constant CHOP where its value is the following expression:

op('keyboardin1').fetch('count', 0)

This will fetch the current count value stored in keyboardin1.

I understand now, I also found some video from Matthew Raegan on youtube on storage also. Thank you again for the helpful advice, its has pushed me along alot!