% Modulo Function Explanation

Since I spent almost a day hunting for the mysterious "%" to find out what it means and how to use it, I thought I share this super helpful explanation by @firemaplegames with all the other noobies and math failures like me still scratching their heads.


the modulo operatator, %, is used to find the remainder after division.

so: 10 % 3 = 1
because 3 goes into the number 10 exactly 3 times, with 1 left over.

and 8 % 4 = 0
because 4 goes into the number 8 exactly 2 times, with 0 left over.

and 17 % 6 = 5
because 6 goes into the number 17 exactly 2 times, with 5 left over.

Well, that's all well and good you say, but how could I possibly benefit from that?!?
That seems like the most useless math function. I could not ever possibly imagine using it!

Ah! But the mod function is very powerful, and used all the time in computer programming:

Here are a few quick examples:

It is used to quickly tell if a number is even or odd. Like this:

(ANYNUMBER) % 2
This will return 0 for an even number, 1 for an odd number.
In GameSalad, it would look like this:

Change Attribute: game.evenOrOdd To: myNumberToTest % 2

When game.evenOrOdd = 0
Display Text: "This number is Even!"
Otherwise
Display Text: "This number is Odd!"

Using that same exact logic, programmers use it to create "zebra stripes" in html tables. I'm sure you've seen a table of data that has alternating white and light gray lines. Your iTunes music library, for example. (although it's very subtle) uses this.

That effect is created like this:

If myCurrentRow % 2 = 0
Use the light gray background
Otherwise
Use the white background

The same logic again for a "toggle" switch:

Make an integer attribute. game.Switch

When you press the mouse button, or touch(pressed) the actor:

Change Attribute: game.Switch = (game.Switch + 1)%2
So the value of Switch is either 0 or 1. You can check for those values.

Another very handy use of the modulo is to use if for setting a limit on something.
This is used a lot in games as well.

Let's say your character collects coins in the game. Like in Super Mario Bros.. Mario can collect 99 coins, then the counter resets to 0, and it keeps going like this forever.

Well the "coin limit" is 99.
And you can use the modulo like this:

So every time Mario gets a coin, do this:
game.totalCoins = game.totalCoins+1%99

Every time the coin counter hits the limit, in this case 99, the counter resets to 0

But it doesn't just have to be a number, it can be an actor's width, or its alpha. anything, really:

self.maxWidth = 250
self.size.width = self.size.width+10%self.maxWidth

Or how about this:
In Asteroids, I want to give the player a bonus ship every 20,000 points

You would do that like this:

If currentScore%20000 = 0
Award a bonus ship

This will happen at 20000,40000,60000 etc...

Or if you're mean:
I want Space Invaders to get harder for every 2 minutes that the Player is at the machine:

if currentTimePlayedinSeconds%120
AlienFireRate=AlienFireRate+1

Or for laying out a grid, or chessboard, in a puzzle game
(you would use this in a loop)
gridSquareX = (currentGridSquareNum%numColumns)*gridSquareWidth
currentGridSquareNum = currentGridSquareNum+1

That should give you some basic ideas!

Hope this helps!
Joe


I found it here: http://forums.gamesalad.com/discussion/1885/no-mod-funtion

I also found it helpful to try the examples with a calculator at hand http://calculator.sdsu.edu/calculator.php
because the first examples
10 % 3 = 1
and
(any number)%2 = odd or even
that was easy to understand but the 99 coins example still had me scratching my head.
Lightbulb flashing when I found out that e.g 70%99 = 70

Hope somebody else makes this as happy as it made me.

I really would love it if there was a section in the cookbook that had such helpful examples for every operator and function.
Or maybe there could be a section in this forum entirely for that? Just threads tend to be bit unorganized to find stuff when you need it. Any ideas how to make something like this happen?
Thank you Joe!!

Comments

  • firemaplegamesfiremaplegames Member Posts: 3,211
    Glad it helped you out 4 years later!
  • krustelkramkrustelkram Member Posts: 19
    It for sure did! Still having some trouble wrapping my head around it completely, but your explanation and examples really helped shedding some light - even if it still suddenly goes dark from time to time :)
  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    there is also the mod(x,y) function which is the same as x%y.
    Please use mod instead. '%' might get used for other things in other platforms, and since we can't totally phase it out since it could break your older projects, it is less prone to other issues if you use mod(x,y).
  • krustelkramkrustelkram Member Posts: 19
    Thanks for the info @CodeMonkey and uhh now I think I have a knot in my brain, but
    let's see if I can get it right:
    Change Attribute: game.Switch = (game.Switch + 1)%2
    would be:
    game.Switch = mod((game.Switch + 1),2)
    and
    self.size.width = self.size.width+10%self.maxWidth
    would be:
    self.size.width = mod( self.size.width+10 , self.maxWidth )
    Is that right?
    And is it allowed to put some spaces where the comma is inside the brackets? get's so hard to see what's going on like this.

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    correct. Spaces don't need to be there. Usually you can't put spaces in the expression editor unless you forcefully add one.

    Also you can do this
    game.Switch = mod(game.Switch+1,2)
    with less parenthesis.
  • krustelkramkrustelkram Member Posts: 19
    Thank you!
  • colandercolander Member Posts: 1,610
    @CodeMonkey It would be helpful if the mod(Function) was listed in the Cookbook Definitions http://cookbook.gamesalad.com/definitions could you get it included?
Sign In or Register to comment.