How to limit how far a ++ or -- function can go?

I have a music track switcher function that works like this:

The music list looks like this:

Works well, but unfortunately, if I keep pressing ‘next’ track, it will go on forever, and each button press registers a ‘next track’ even though it doesn’t exist after 5. So when you want to go back to a previous track, I have to press as many times as I pressed next until it actually goes back to the previous track.

I added a CLAMP INTEGER function to be 0-5 before the ++ and – but that didn’t work. How do I hard-limit the range so the ‘next’ and ‘prev’ buttons stay within 0-5?

You could use the wrap (integer) node to wrap around the values once you hit the max.

2 Likes

thank you for that! you rock! this is how I ended up:

worked like a charm!

i think if you wanna clamp it, there could be something like max and min or clamp.

1 Like

I’d go with Clamp. Wrapping literally wraps. If the input value exceeds the maximum, the value “wraps around” from the beginning of the range.

For example, if the range is 0-100 and the input is 101, the output is 1. If the input is 110, the output is 10.

0-5 … 5.2 = 1, 8.0 = 3

1 Like

thanks, understanding the diff between clamp and wrap is important.

1 Like

In the case of music though going to the next track after the last logically would go to the first track again, otherwise you would constantly be playing the last track once you hit it.

I think context of the situation would suggest a loop, as it mirrors how digital music players work when shuffle is disabled.

2 Likes

I was just defining Clamp over Wrap. Personally I’d conditional check the Track and manually set as needed.

Next: If (Track == 5) { Set Track 1 } Else { Increment (Track) }
Prev: If (Track == 1 ) { Set Track 5 } Else { Decrement (Track) }

2 Likes

I appreciate the detailed look into this. For me both options are good to understand and think about. I might have to use clamp sometime anyway. And yes, @3dRaven the wrap worked nicely here because it does mimic the way digital players work :dvd:

1 Like

Just wanted to drop this , exactly stepper logic like mentioned above.

You can just change function in the end to play a new track on loop or set parameter to cue.

You can go even further by putting the code in a function and providing an input of Fwd/Bwd to control switch logic.

2 Likes