Modulo Operations in Smile Game Builder

Modulo Operations in Smile Game Builder

A Modulo operation finds the remainder after dividing one number by another number. It’s better known as mod or modulus, and in some programming languages, such as C++ and JavaScript, it’s represented by a percentage sign (%).

Substitute Divided Remainder
Substitute Divided Remainder
To output individual parts of numbers – 1000’s, 100’s, 10’s and 1’s – for such things as HUDs to display numbers in a graphical representation, Smile Game Builder uses Substitute Divided Remainder in the Advanced Variable Box Op, which is the modulo operation.

So, say you wanted to use the 10’s value from a number, but not the 1’s or any remainders after it, you can extract it as a single value using this function. In this article, variable manipulation uses the Advanced Variable Box Op.

Random Number

For the purposes of this article, and to see it in action, the first step is to create a variable to generate a random number.

  • Use the Random Number command in the Advanced Variables to generate a random number range. The regular Variable Box will just add a random number to the existing number. However, we want to generate a new random number each time.
  • Assign a variable with a random number between 0 and 999.

Units or 1’s

To calculate the units, the formula is:

   Number % 10

Which simply divides the number by 10 and outputs the remainder.

  • Store the random number variable into another variable for the 1’s.
  • Add another Advanced Variable Box to modify the 1’s variable to a fixed value of 10, and set the Substitute divided remainder as the operation.

Tens or 10’s

For caluclating the tens, the modulus operation is:

   (Number % 100) / 10

  • Once again, set a variable for the 10’s to the same as the random number.
  • Substitute the remainder with a fixed value of 100.
  • And divide the result by 10.

At this point, you may start to see a pattern emerging. This is a surefire way of calculating and outputting much larger numbers…

Hundreds or 100’s

The formula for the hundreds is:

   (Number % 1000) / 100

  • So, as with the tens, a variable is created, this time for the 100’s, and set to the same as the random number.
  • The remainder is set.
  • And divide the result by 100.

Thousands or 1000’s

If you’re going to use higher numbers in HUDs, such as for stats, you’ll also need thousands as well.

The modulo operation to calculate this is:

   (Number % 10000) / 1000

This is set up in the same way as the previous numbers using the variable to store the 1000’s.

Final Product

The final product will look like this:

Modulus Operation Final Product
Modulus Operation Final Product

Each time the random number updates, it’ll separate the value into its component parts, returning an integer for the thousands, hundreds, tens and units so that they, in turn, can be used to update graphics referencing the number.

You can check to see if it’s running properly by pressing F5 during playtesting and monitor each variable.

Notes About HUDs

I doubt you’ll need more than the 1000’s for creating HUDs because if numbers exceed the maximums, they won’t go above that. The only exceptions are probably experience and gold. If you’re determined on using these in a HUD, then following the same pattern by adding zeros to numbers (as seen above).

I won’t go into actually creating HUDs here, as that would take an entire tutorial – perhaps more than one – to cover fully. However, perhaps I will some time in the future.

There are a number of issues with HUDs, notably when numbers refresh on the map. They tend to flicker on-screen, which is distracting and annoying. This is primarily due to the way that SGB uses and synchronizes auto-running events. It cycles through each one until conditions are true, but removes and then replaces the graphical numbers according to those conditions.

Additionally, each true condition overrides the previous ones, so another anomaly occurs with trailing zeroes. As an example, if your value is three digits long, the output on-screen should be 000. However, if the number is 7, the preceding zeros are displayed as blanks.

There is, of course, a workaround for this, but it isn’t reliable or efficient. I’ll put that in another tutorial some time in the future when, hopefully, I’ll have a proper fix for it.

Author: Companion Wulf

Leave a Reply