Home > Tutorials > RPG Maker XP > Use of Mods
Use of Mods 
Use of Mods: Convenient digits
From a thread that I helped
Description:
Ever wondered what the operation of mod in the variable means? It basicly divides the number and the remainder of the answer will be stored into the variable. Mod is just part of this tutorial but this tutorial mainly focuses on the use of operations to get a huge number and turn them into seperate digits.
Example:
486 Is a whole number, but if you use this tutorial you would be able to take each digit. Which means you would be able to seperate them into digits of 4, 8 and 6.
Application:
So what is the use you might ask? Simple, you would be able to display numbers through pictures without using many conditional branches. Which means, displaying them by each digit not by whole numbers. Convenient eh?
Let's start
Here is the raw code. Explanations Below.
Code:
Code:
Variable A = Rand 1..99
Variable B = Variable A
Variable C = Variable A
Variable B/= 10
Variable C %=10
Result:
B- tens digit
C- ones digit
1)Since we set A to any random number between 1 through 99, we should get any number from 1-99 right? Now suppose we get 45 as a random number.
2) Now we need to get the tens digit first right? We also don't want to place operations to variable A yet since we need to use the value of A to get the ones digit also. So what we do is set another variable to get A, which we call Variable B
3) Now we also need to get the ones digit right? And we also don't want to place operations on B or A because they have their own purpose, B for the Tens digit and A for the original number. So we Make another variable that equals to A to get the ones digit. Which we call Variable C
4) So first to get the tens digit, we simply divide the number by 10. And since Rpg maker does not take any decimals, we get no decimals but a whole value. Now using 45 as our example, 45/10 = 4. So our tens digit is now 4.
5) Now that we have 4 as our tens digit, how do we get the 5? Simple. We get the remainder when 45 is divided by 10. But wait, you might say that I just have stated that Rpg maker does not take any decimals, but it does take remainders. HOW? We use the operation called mod (%) this divides the number and whatever the remainder is, that will be the value after the operation. So 45/10 = 4 remainder 5, the rpg maker will take 5 and store its value.
6) And so we have 4 as the tens value and 5 as the ones value.
Variable A= 45
Variable B= 4
Variable C= 5
That's for a 2 digit number what about 3 digit numbers?
Suppose we have 192 value and we want to get each digit out of it, what do we do?
Code:
Quote:
Variable A = 192
Variable B = Variable A
Variable C = Variable A
Variable D = Variable A
Variable B /= 100
Comment: B = 1
Variable C %= 100
Comment: C = 92
Variable C /= 10
Comment C = 9
Variable D %= 10
Comment D = 2
I hope the code was self-explanatory, now what about 4 digit numbers?
Suppose we have 1923 as our sample:
Quote:
Variable A = 1923
Variable B = Variable A
Variable C = Variable A
Variable D = Variable A
Variable E = Variable A
Variable B /= 1000
Comment: B = 1
Variable C %= 1000
Comment: C = 923
Variable C /= 100
Comment C = 9
Variable D %= 100
Comment D = 23
Variable D /= 10
Comment: D = 2
Variable E %= 10
Comment: E = 3
Comment: Result
A= 1923
B = 1
C = 9
D = 2
E = 3
The 2 codes should be self-explanatory.
Quote:
It takes 1 operation to get the highest place value (Divide the place value ex:192, Divide by 100 = 1)
Then it takes 2 operations to get either any other place value lower than the highest place value.(Mod then divide)
Finally it takes 1 operation to get the once digit. (Mod by 10)
Quote:
Example:
1,987
(For thousands)Divide by highest place value (1000) = 1
(For hundreds)Mod by 1000 = 987 Divide by 100 = 9
(For Tens) Mod by 100= 87 Divide by 10 = 8
(For Ones) Mod by 10 = 7
More Examples:
23,541
(For ten thousands) Divide by highest place value(10,000) = 2
(For Thousands) Mod by 10,000 = 3, 541 Divide by 1000 = 3
(For Hundreds) Mod by 1000 = 541 Divide by 100 = 5
(For Tens) Mod by 100 = 41 Divide by 10 = 4
(For Ones) Mod by 10 = 1
|
|
Details
|
|
Tutorial:
|
Use of Mods |
|
Date Listed:
|
Thu, 03 Jul 2008 01:36:28 -0700 |
|
Author:
|
jens009
|
|
Total Hits:
|
2128 |
|
|