How do I use MODULO in programing or eventing?

0 Favourites
• 4 posts
• Hi,

A while back I saw a use of modulo where it was placed at the end of equation like this: % 4. where it was suppose to return some other number if random number would be bigger then 4. But I don't exactly get it, so if anyone could give me some tips on how to use it, that would be appreciated. Thanks!

• Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Modulo gives you the rest that occurs when dividing numbers.

e.g.

17 % 3 = 2 (17 = 5*3 + 2)

3 can be inside of 17 5 times, what is left is 2 (since its not divisible by 3 for putting out natural numbers).

Modulo will always give you that "rest" number. In case the number on the left is lower, you will always get the number on the left back.

1 % 4 = 1

2 % 4 = 2

3 % 4 = 3

4 % 4 = 0

5 % 4 = 1

You could (just an example) use it for determining every so and so number of elements in a loop.

Imagine having 12 enemys that you want to split to the left and right via a loop every 3 enemies changing the side. (yes i know, quite made up that example).

You would go check for each loop index + 1 % 3 equals 0 and change the sides.

edit: sorry guess you knew what modulo is about. Go by MadSpys examples on how to use it.

Guess that really depends on what logic you want to do, just sometimes modulos might be the answer you need.

• Thanks guys!