# How Does the Length of Built Objects % Summon Types Work?

Hello, guys. Does any of you know exactly how

``````summonTypes[len(hero.built)%len(summonTypes)]
``````

works?

So, first of all you have the array of types, which each have an index (from 0, to however many items you have in the array - 1, because it started at 0, not 1) e.g.

``````summonTypes = ["archer", "soldier", "peasant", "paladin"]
0          1          2          3
``````

So len(hero.built)%len(summonTypes) will always return a result within 0-3.
We know that len(summonTypes) will always equal 4, because the number of items in the array won’t change in this level. So that side of the operation is fixed.
len(hero.built) on the other hand will increase from 0 to however many units you build.
Finally, let’s look at the most important bit, the % (modulo) operator. What % does is it returns the remainder of a division. Here the division is len(hero.built) divided by len(summonTypes). So you find out how many times len(summonTypes) can go into len(hero.built), then return the gap between that number, and what len(hero.built) actually is.
For example, imagine you haven’t built any troops.
So len(hero.built) is 0, and, or course, len(summonTypes) is 4.
So, % will find the remainder of 0 divided by 4. 0 divided by 4 is 0 and has no remainder, so that means it will return 0, which is “archer”.
Next time you have enough gold to summon something, len(hero.built) will be 1, and len(summonTypes) will still be 4.
So 1 divided by 4 is what? 4 goes into 1 zero times, with a remainder of 1. So the modulo function will return 1, which is “soldier”.
I hope this helps you understand it.

1 Like

So, what happens when the `len(hero.built)` goes over 4?

Well, first let’s imagine it with 4.
How many times does 4 (len(summonTypes)) go into 4 (len(hero.built)). The answer is once exactly, with no remainder. So modulo would return 0, meaning the type would be “archer” again.
Then, with 5. How many times does 4 go into 5? Once, with a remainder of 1, so the type would be “soldier”. This continues upwards, and every time you reach a number that is divisible by 4, the index gets set back to 0, because there’s not remainder to the division.

1 Like

Thanks for the explanation!  This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.