Timer up, Timer Down (w/ Interval control)

Greetings, and sorry for the simple question. After a bunch of searching, I couldn’t quite solve it and I’m still in the early phases of learning C# …

I’d like the attached C# script to have the added functionality of an Interval input, allowing the count speed to be adjusted in ms – as in, for example, the attached Python component found here:

Currently, the script simply counts up to a max value, and counts back down to 0 when reached (and back again) - this is good, but I’d like to slow it down and speed it up without having to overshoot the max and divide the output.

This post has given me a clue as it relates to the above, but I’m a bit stumped on how to bring this into to attached C# component:

Timer_UpDown.gh (7.5 KB)
for reference: 191228_GHPython_Timer_01.gh (5.2 KB)

private void RunScript(bool Run, bool Reset, int Interval, ref object Counter)
{
    if (Reset)
        _myCounter = 0;
    else if(Run)
    {
        _myCounter++;
        UpdateComponent(Interval);
    }
    Counter = _myCounter;
}

// <Custom additional code> 
int _myCounter = 0;
void UpdateComponent(int interval)
{
    GrasshopperDocument.ScheduleSolution(interval, doc => Component.ExpireSolution(false));
}
// </Custom additional code> 

InternalTimer.gh (4.9 KB)

This is helpful, and I’m sorry for the confusion. I’m looking to keep the C# script as-is, but add the interval functionality. I only posted the Python version as a reference for what I’d like to add, but I don’t want it to replace the max value input and the counting down once it’s reached.

Sorry I didn’t check the attached file.

private void RunScript(bool reset, bool run, int max, int interval, ref object A)
{
    if(reset) n = 0;
    else if(run)
    {
        n += increase ? 1 : -1;
        if(n == max || n == 0) increase = !increase;
        UpdateComponent(interval);
    }
    A = n;
}

// <Custom additional code> 
bool increase = true;
int n = 0;
void UpdateComponent(int interval)
{
    GrasshopperDocument.ScheduleSolution(interval, doc => Component.ExpireSolution(false));
}
// </Custom additional code> 

InternalTimer.gh (6.6 KB)

1 Like

Ah, very nice. Thanks!!

Here’s an example for a use for this - for quick animations within the GH environment.
Edit - instability is fixed: “increase” needed to be set back to true when the reset button is pressed to avoid the value going negative if the button is pressed during a decrement.

WavyHyperboloid_v2 animate.gh (23.7 KB)

1 Like