02.01.2014, 20:02
(
Last edited by Hansrutger; 03/01/2014 at 12:56 PM.
)
Modulo Operator in Pawn
Preface
I'd like to begin to say that this is not an ordinary pawn scripting tutorial. This is a simple tutorial covering the modulo operator. When I searched on the forums with the word "modulo" on thread search I only found three threads where one was a tutorial in Portuguese, and two where "help me" threads so I decided to write this, even though if you guys find it bad, I tried my best to explain with my own words and with my knowledge from last semesters "Discrete Mathematics". This tutorial will most be math but "format" might show up.
Skip to next title to get to know how to use modulo in pawn language.
Mod what?
Modulo is an operator in most scripting languages and in mathematics just like addition (+), subtraction (-), multiplication (*) or other operators. They all have different functions. The modulo operator gives us the so called "rest" from a division. In the world we live in, we have no modulo. That is although wrong, we do have a mod but it's infinity. This is where it begins to struggle with most people. The mod tells us when to stop counting and start back again from 0.
Let's jump into some examples before it gets too annoying:
Code:
11 mod 6 ≡ 5 12 mod 6 ≡ 0 13 mod 6 ≡ 1 Figure 1
Code:
11/6 = 1,8333333333333333333333333333333 12/6 = 2 13/6 = 2,1666666666666666666666666666667 Figure 2
Code:
Fractional part examples: 0.5 = Fractional part is 0.5 1.2 = Fractional part is 0.2 5.852 = Fractional part is 0.852 1000000.52 = Fractional part is 0.52 Figure 3
Code:
11/6 = 1,8333333333333333333333333333333 1,8333333333333333333333333333333 - 1 = 0,8333333333333333333333333333333 0,8333333333333333333333333333333 * 6 = 5 (rounded) Answer: 11 mod 6 ≡ 5 12/6 = 2 2 - 2 = 0 0 * 6 = 0 Answer: 12 mod 6 ≡ 0 13/6 = 2,1666666666666666666666666666667 2,1666666666666666666666666666667 - 2 = 0,1666666666666666666666666666667 0,1666666666666666666666666666667 * 6 = 1 Answer: 12 mod 6 ≡ 1 Figure 4
What happens when you use it?
A bit more explained it counts basically like this:
Code:
You have "12 mod 6" as an example: Our counting | modulo counting 0 | 0 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 0 7 | 1 8 | 2 9 | 3 10 | 4 11 | 5 12 | 0 Figure 5
Pawn modulo
To use this "complicated" operator in pawn language we use the operator sign "%". Many languages uses this such as C#. To my previous examples, it would be written like this:
Code:
Previous: 12 mod 6 Pawn: 12 % 6
Example 1)
You are in ajail because you broke the server rules and you want to know the time of still being in the ajail, so you contact the server scripter to script this. He would, in my honest opinion, write the code the best way by doing something like this:
pawn Code:
new minutes, seconds; //declare two integers (look E1)
new str[128]; //declare a string to hold everything
minutes = seconds / 60; //to get minutes you simply divide seconds / 60 as there are 60 seconds in one minute (easy math)
seconds = seconds % 60; //calculate modulo (look E2)
format(str, sizeof(str), "Time left: %i,%i", minutes, seconds); //using format to format everything into "str"
SendClientMessage(playerid, -1, str); //sending a message
E2: Each time you have 60 seconds you want it to turn downwards into 0 right? Because 61 seconds doesn't exist in minutes (it does but you know...), so it will instead write 1 second instead of 61 seconds. So you take the float minutes and use modulo "%" with 60.
Discovery: Apparently by default if you do "minutes = seconds/60" minutes will always round downwards, so therefore no need to use "floatround".
Example 2)
You want to make so each time someone hits a 10-year anniversary you want to send message to congratulate them.
pawn Code:
new age, an; //declare a new float and an integer called "an"
an = age % 10; //calculate the age in 10-year anniversary terms (look E1)
if(an == 0) //asks if an is 0 (look E2)
{
SendClientMessage(playerid, -1, "Congratulations on your anniversary!");
}
E2: As we said, we wanted to make a congratulations each time someone turns a 10-years anniversary. We ask if an is equals to 0. If it is, it means the person is either 10, 20...etc. If not, it doesn't go inside the if-statement.
Additional notes:
Credits goes to e for writing the tutorial. All words are written by me. I really hope that this opens up a bit more knowledge to people reading this tutorial at least. I did not want to explain format or SendClientMessage more than I did because I didn't feel that it was important in this thread!
Credits for fixes:
pds2k12 - example code fix
Vince - English word fix