Using variables in callbacks

Hi,

I hope it will be my most newbie question ever.

CHOP Execute, magic way for doing a lot of things. ok.

I (just) need the way to declare variables that I could use in my callbacks.
I tried global < varname > at the top of the script… doesn’t work.

I miss something in python, but in that case too.
I tried to find the information parsing some snippets and scripts, but I didn’t find my way.

Thanks a lot.

answering to myself, in my case, the thing was JUST to use the global tag in the callback function itself:

n = 0

def onValueChange(channel, sampleIndex, val, prev):
	global n
	if (n <3):
		n+=1
	else:
		n=0
		op('count1').par.resetpulse.pulse()
	return

And voila!

It is for a kind of automatic resetting counter.
It is a Counter CHOP + a CHOP Execute DAT.

Maybe it would be better to do the count directly inisde the CHOP Execute DAT (I’m still exploring)

1 Like

One interesting piece to consider here is that this limits the availability of this variable only within the scope of this CHOP execute.

So, for example, if you wanted to know the status of this variable in another operator or script you’d be out of luck.

There are a number of possible solutions in this case, you might use table values, a constant CHOP, storage, custom parameters, or class objects (python extensions in TouchDesigner).

One more piece for python help - from the help menu there’s also python examples:
image

Similar to Op Snippets but focus on Pythonic concepts in TouchDesigner:

1 Like

Yes, got it.
I still didn’t need to share variables across different CHOP Execut or python scripts in my network.

I understand the idea of sharing data through Constant CHOP, storage, DAT tables etc. That’s very clear.

And one more thank for the Python Examples item…