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

**URL:** <https://forums.unrealengine.com/t/is-there-an-easy-way-to-have-an-array-index-loop-back-to-zero-when-its-larger-than-the-arrays-length/407624>\
**Category:** Audio\
**Tags:** arrays, unreal-engine, looping, Blueprint, question\
**Created:** [October 22, 2017, 4:35am UTC](https://forums.unrealengine.com/t/is-there-an-easy-way-to-have-an-array-index-loop-back-to-zero-when-its-larger-than-the-arrays-length/407624 "2017-10-22T04:35:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ecnassianer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ecnassianer](https://forums.unrealengine.com/u/Ecnassianer)\
**Post date:** [October 22, 2017, 4:35am UTC](https://forums.unrealengine.com/t/is-there-an-easy-way-to-have-an-array-index-loop-back-to-zero-when-its-larger-than-the-arrays-length/407624/1 "2017-10-22T04:35:04Z")

</div>

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

 ![217510-overflow.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/2/c/52c9cd0884ab29d660e346f9043d9c3ffbd57c04.jpeg)

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

 ![217521-overflowresult.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/2/7/c277bceebcab45aa69a7b60c41292a5fc8ab628d.jpeg)

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:

 ![217522-resetoverflow.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/c/a/fcac2b1ac2d3ab4a241582ec12c08a7dab6829b1.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8190d78f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8190d78f](https://forums.unrealengine.com/u/anonymous_user_8190d78f)\
**Post date:** [October 22, 2017, 6:23am UTC](https://forums.unrealengine.com/t/is-there-an-easy-way-to-have-an-array-index-loop-back-to-zero-when-its-larger-than-the-arrays-length/407624/2 "2017-10-22T06:23:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![emperor\_katax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emperor_katax/32/1070178_2.png) [@emperor\_katax](https://forums.unrealengine.com/u/emperor_katax)\
**Post date:** [March 14, 2024, 12:51pm UTC](https://forums.unrealengine.com/t/is-there-an-easy-way-to-have-an-array-index-loop-back-to-zero-when-its-larger-than-the-arrays-length/407624/3 "2024-03-14T12:51:51Z")

</div>

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

 ![mod-loop](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/b/6/3b69b43873b16968883bef2459e9bd8c51bba9a5.png)
