sexta-feira, 3 de março de 2017

BP Compendium 3: Modulo (%)

Modulo (%)

The "Modulo" operation that is represented by the symbol "%" returns the remainder of a division.

  • A: Value to be divided.
  • B: Value that will be used as the divisor.

  • Return Value: Remainder of the division.

Example Usage: 

The "Modulo" operation with the integer type can be used to control the execution of certain actions that should only occur after an event is executed N times. In the example below there is an event that represents a new wave of enemies. There is an integer variable called "WaveCount" that keeps the current number of the wave of enemies. The "Modulo" is used to check when this variable is a multiple of 5. This is done by verifying if the remainder of the division of this variable with the value 5 is equal to zero. If this happens, a new type of enemy is created in this wave.

Click to enlarge

Another situation where it is common to use the "Modulo" is to convert the index of a one-dimensional array into a two-dimensional coordinate. For example, an array of integers is being used to represent a chessboard. The array contains 64 positions, so its indexes range from 0 to 63. Each position of the board is represented by a coordinate (X, Y). The first position has coordinates (X = 1, Y = 1) and the last position is (X = 8, Y = 8). The macro below converts an array index to the (X, Y) coordinate of the board. 

Click to enlarge

Table of Contents