@scitunes: 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:
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
Wow that was a very long explanation! Or in my case, I am using it to show the score in my own font, and it is needed to tell which digit is in which place of the score
Firemaple, very nice explanation. Sounds like you have a lot of math knowledge. Since tshirtbooth was doing videos, anyway we could convince you to do some more math explanations for those of us that are mathematically challanged?? cough..cough....quantum...cough..cough... You know like all the other math functions that I can't even pronounce much less figure out how to use....
Honestly guys, a lot of these functions are used in Geometry and advanced maths for determining the angles and length of triangles (sin, cos, tan, atan, acos, asin), and expecting to not just learn, but apply them in a game is a bit much.
If you had a specific situation you needed help with that would be easy, but an explanation of how to use them is a lot.
firemaple, would it be possible for you to upload a project that has one actor that when clicked on the first time changes to blue and then when clicked on a second time changes to red - ie a toggle swith that changes from blue to red and back again. I'm assuming that this would use the mod thingy.
If I'm being too much of a pain then don't worry about it. You just seem so good at this stuff! Thanks for all of the info you have been sharing so far - it has really helped!
Scitunes, you could just use a boolean for that. Create a boolean attribute on an object called isRed and set to false.
When you click on the object, make a rule that: If isRed is false, change color to red and set isRed to true Otherwise change color to blue and set isRed to false
@scitunes: Thanks for the kind words! A toggle switch like that is very easy. Like Eastbound said, you would simply need a Boolean attribute in the actor called something like "isRed", and two nested Rules in the Actor like this:
Rule When Mouse Button is DOWN When Mouse Button is INSIDE Rule When self.isRed = TRUE Change Attribute: self.isRed To: FALSE Change Image to the Blue Image Otherwise Change Attribute: self.isRed To: TRUE Change Image to the Red Image
No, the mod way of doing that is similar, with the advantage that it is easy to add colors and cycle through them. So maybe down the line you want to add an orange color, so now when you click on the actor it cycles from red to blue to orange. To just make a simple toggle switch, it would look like this:
You would need two Integer Attributes in the Actor one called something like totalNumberOfColors, and one called currentColor.
In this case, we'll set totalNumberOfColors to 2, since there are only two colors, blue and red.
So in the Actor:
totalNumberOfColors = 2 currentColor = 0
And a rule to set the currentColor:
Rule When Mouse Button is DOWN When Mouse Button is INSIDE Rule Change Attribute: self.currentColor To: self.currentColor + 1)% totalNumberOfColors
In this case, every time you clicked the actor, its currentColor attribute would keep toggling between 0 to 1
Then you need a separate Rule for each color like this:
Rule When self. currentColor = 0 Change Image to Red Image
Rule When self. currentColor = 1 Change Image to Blue Image
Now, let's say you wanted to add Orange into the mix. By doing it this way, it's easy to add a new color:
Change totalNumberOfColors to 3
And Add a new Rule:
Rule When self. currentColor = 2 Change Image to Orange Image
If you had built it with the original way, adding orange would be a pain. You would either need two more booleans, isBlue and isOrange, which gets ugly fast, or you would need to rip apart the original function and build new rules, etc. This way is much more flexible.
Comments
The percent sign?
Because you CAN use % in the expression editor.
It works fine.
It's just not in the drop down menu...
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
How would that work, Mr. Eastbound?
Cheers!
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io
i just like the logic of it all.
but if there is a math term that you don't understand, I'll try to figure out a way that it can apply to something useful in GameSalad.
If you have any suggestions, please let me know!
Joe
(sin, cos, tan, atan, acos, asin), and expecting to not just learn, but apply them in a game is a bit much.
If you had a specific situation you needed help with that would be easy, but an explanation of how to use them is a lot.
If I'm being too much of a pain then don't worry about it. You just seem so good at this stuff! Thanks for all of the info you have been sharing so far - it has really helped!
When you click on the object, make a rule that:
If isRed is false, change color to red and set isRed to true
Otherwise change color to blue and set isRed to false
Rule
When Mouse Button is DOWN
When Mouse Button is INSIDE
Rule
When self.isRed = TRUE
Change Attribute: self.isRed To: FALSE
Change Image to the Blue Image
Otherwise
Change Attribute: self.isRed To: TRUE
Change Image to the Red Image
No, the mod way of doing that is similar, with the advantage that it is easy to add colors and cycle through them. So maybe down the line you want to add an orange color, so now when you click on the actor it cycles from red to blue to orange. To just make a simple toggle switch, it would look like this:
You would need two Integer Attributes in the Actor one called something like totalNumberOfColors, and one called currentColor.
In this case, we'll set totalNumberOfColors to 2, since there are only two colors, blue and red.
So in the Actor:
totalNumberOfColors = 2
currentColor = 0
And a rule to set the currentColor:
Rule
When Mouse Button is DOWN
When Mouse Button is INSIDE
Rule
Change Attribute: self.currentColor To: self.currentColor + 1)% totalNumberOfColors
In this case, every time you clicked the actor, its currentColor attribute would keep toggling between 0 to 1
Then you need a separate Rule for each color like this:
Rule
When self. currentColor = 0
Change Image to Red Image
Rule
When self. currentColor = 1
Change Image to Blue Image
Now, let's say you wanted to add Orange into the mix. By doing it this way, it's easy to add a new color:
Change totalNumberOfColors to 3
And Add a new Rule:
Rule
When self. currentColor = 2
Change Image to Orange Image
If you had built it with the original way, adding orange would be a pain. You would either need two more booleans, isBlue and isOrange, which gets ugly fast, or you would need to rip apart the original function and build new rules, etc. This way is much more flexible.
Hope this helps!
Joe