This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this Introduction To ChoiceScript Game Development series of linked articles: A Basic Tutorial.
Manipulation of numbers is a fundamental part of all programming and scripting. This article will discuss the correct format by which we can apply these mathematical methods to both previously stored and new data.
There are several arithmetic operators you can use as a ChoiceScript game developer.
Basic math operations
Let's say we have a numeric variable called var with a current value of 50, to which we apply any one of the following operations:
*set var + 5
A var with a value of 50 would now have a value of 55.
The current value of one variable can also be added to another:
*set var1 + var2
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 55. The value of var2 remains unchanged.
*set var - 5
A var with a value of 50 would now have a value of 45.
The current value of one variable can also be subtracted from another:
*set var1 - var2
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 45. The value of var2 remains unchanged.
*set var * 5
A var with a value of 50 would now have a value of 250.
The current value of one variable can also be multiplied by another:
*set var1 * var2
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 250. The value of var2 remains unchanged.
*set var / 5
A var with a value of 50 would now have a value of 10.
The current value of one variable can also be divided by another:
*set var1 / var2
If var1 had a value of 50 and var2 a value of 5, var1 would now have a value of 10. The value of var2 remains unchanged.
This operator is a little different than the others; it can be used on variables from 1 to 99 (inclusive), with the error reporting that it can only be used on "percentile values". Fairmath is of two types, "%+" and "%-".
They are used this way:
*set var %+ 20 *set var %- 20
The idea of fairmath is that the closer a variable is to 100 (the higher it is), the harder it is to increase, and the closer a variable is to 0 (the lower it is), the harder it is to decrease:
Fair Addition: (x %+ y) = (x + (100-x)*(y/100))
- High variables are harder to increase: (90 %+ 20) = (90 + 2) = 92
- Low variables are easier to increase: (10 %+ 20) = (10 + 18) = 28
Fair Substraction: (x %- y) = (x - x*(y/100))
- High variables are easier to decrease: (90 %- 20) = (90 - 18) = 72
- Low variables are harder to decrese: (10 %- 20) = (10 - 2) = 8
50 is equally easy to increase or decrease, as you've seen above.
The *round() is used for setting a variable to the nearest integer, as following:
*set var round(30.5)
this will automatically set var to 31.
You can also use it with variables instead of numbers, as following:
*set var1 round(var2)
where var2 is equal to 20.5; var1 will be equal to 21.
Remember, never make any spaces between "round" and the "(var1)" brackets.
The modulo is used to find the remainder when one variable is divided by another. It is used simply typing the wird "modulo" between the first variable (the dividend or numerator), and the variable you want to divide by (divisor or denominator). It is a fairly obscure and weird command, but useful in two main ways. First, you can check if a number is evenly divisible to another by writing:
*if (var1 modulo var2) = 0
For example, we can try:
*if (50 modulo 10) = 0
which is true, because 50 divided by 10 leaves no remainder, but if we try:
*if (50 modulo 40) = 0
it is false, because 50 divided by 40 leaves 10 as a remainder.
Secondly, we can also get the fractional part of a number, so that if we do this:
*set var 50 *set var / 40
the result will be 1.25; then if we do:
*set var modulo 1
the result will be 0.25.
Note that the above example rewrites the variable var when the module is used. The quick way around this is to create a second variable, set it to equal the first, then use the modulo. So if we have var1 set to 50, and var2 set to 40 and want to see what the remainder if we divide var1 by var2, we'll need something like this:
*temp var3 *set var3 var1 *set var3 modulo var2
This will leave us with the var1 and var2 intact, and additionally var3 which is 10 (the remainder of 50 divided by 40).
N.B. When used with an *if, the modulo requires parentheses. However, when used with a *set, it requires that there be no parentheses.
Note: As of October 2016, the operator for a modulo operation was changed from the percent sign (%) to the word "modulo". The change was made to avoid confusion with the more commonly used fairmath operations that also use the percent sign.
- ↑ Dan Fabulich, ChoiceScript’s modulo operator is changing from “%” to “modulo”, October 14, 2016.