Counting Changes

I have an external source switching from false to true, and if it detects an object it updates a number - how can I count each time the number changes? I tried python with a global() variable… no luck?

Hi @karls,

This script should work:

from scriptcontext import sticky

if "changes" not in sticky.keys():
    sticky["changes"] = 0

if "current" not in sticky.keys():
    sticky["current"] = Input

if Input != sticky["current"]:
    print "Value Change"
    sticky["current"] = Input
    sticky["changes"] += 1
    
if Reset:
    if "changes" in sticky.keys():
        sticky.pop("current")
        sticky.pop("changes")

if "changes" in sticky.keys():
    c = sticky["changes"]

count_changes.gh (14.2 KB)

1 Like

you’re a legend, second time you’ve helped me!

Thanks, @karls. :blush: I’m also a fan of your YouTube videos.

Hi Karls, it helps with debugging if you provide an example of what didn’t work. Anywho, one really doesn’t need sticky to make persistent variables in GHPython (and if one does use it, it’s probably best to append the component guid onto the variable key name in the sticky to prevent unexpected behaviours). Here’s a simple implementation using the if "foo" not in globals() method:

200301_GHPythonCounter_NoSticky_00.gh (3.2 KB)

If you need a dynamic counter, have a look at this implementation:

1 Like