Is there an easy way to have an array index loop back to zero when it's larger than the array's length?

Very often I find myself using this pattern:
Every time an event fires, I increment an integer:

Then I use that integer to access some element of an array:

That’s great, until my integer gets larger than the length of my array. I have to do this whole rigamarole to get it back to zero when necessary:

Is there an easier way to loop an integer back to zero once it gets larger than some number (for instance, the length of an array). I haven’t been able to find anything in the docs. Basically it’s just a macro for the third screenshot above.

Modulo! I haven’t used it in blueprints, but I’m sure there’s a node for it. It’s an operator that returns the remainder, in code it’s normally represented by %.

So if you do “Index % 3”, then it’ll just return 3 because we haven’t hit the number yet. But when when Index = 5, it’ll loop back to 0. Index = 6 returns 1, 7 returns 2, etc. Easiest way to get used to it is just to play around with a calculator that can perform it.

1 Like

Here on each tick, I can loop through an array of point lights and change the color in an order.