Bit Switches

This script introduces a new type of switch called a “bit switch”. These switches operate on numbers stored in game variables. The purpose is to allow you to group a collection of related switches into a single variable rather than reserving multiple game switches, allowing you to manage larger projects more easily.

A bit switch is a single digit in the binary representation of a number stored in the variable. So for example, the number 6 in binary is written as 110. If we treat each digit as a switch, then we have three bit switches in the number 6. This doesn’t seem like much, but what if we consider a number like 13456730, which in binary is

110011010101010101011010

Suddenly, a single variable can represent 24 switches independently. You could have variables representing 50 or 100 or even 500 switches if necessary. How’s that for saving on switches?

Of course, this script allows you to avoid all of the math and technical details involved (such as the description above), using a simple set of script calls to set switches and check them.

Bit switches have the property that a value of 1 is ON and a value of 0 is OFF. Bit switches are read from right to left, so the first bit switch is the digit on the very right, and the second bit switch is the digit next to it. The number 6 (if you recall, is represented as 110 in binary) means

bitswitch 1 is OFF
bitswitch 2 is ON
bitswitch 3 is ON

We can perform batch switch processing using “bit masks”. A “bit mask” is a collection of bit switches. Rather than checking multiple bit switches separately, we can check them all at the same time. Similarly, we can also set multiple bit switches using a bit mask.

Example

Suppose our variable holds the number 102. In binary, this is 1100110.
If we wanted to know whether bit switch 2 and bit switch 7 are ON, we use the
following bit mask: 1000010. Observe that digits 2 and 7 are ON (why?)

The mask is applied to the variable using a bitwise AND operation as follows

1100110 : value
& 1000010 : mask
= 1000010 : result, equal to mask

If the result is equal to our mask switch, then it will return true.
Otherwise, if even one bit switch is different, then our result is false.

If our variable had a value of 101, or in binary 1100101, our mask would have
failed as follows:

Where the mask is the bit mask you want to use. Note that in this case, a 1 in the mask means that the corresponding bit will have its value changed, so if your mask is 0b0101 with a value of false, then bit switches 1 and 3 will be set to false, while 2 and 4 will remain the same.

In this case, the first mask specifies which bits will be set to ON, while
the second mask specifies which bits will be set to OFF. The 1’s indicate
which bits will be checked, so in the example, bits 3 and 4 are ON, bit 2 is
OFF, and bit 1 is ignored.

The first mask checks for ON bits, and the second mask checks for OFF bits.
The 1’s indicate which bits will be checked by that mask.

Named Bitmasks

This script allows you to assign bitmasks to constants stored in a look up table. You can then reference these constants throughout your events so that if you ever change the bitmasks, you don’t have to go through all your events to make sure that the specific bitmask has been updated correctly.

The bitmask look table is located in the script configuration. The keys are symbols, which represent the name of your bitmask, and the value associated with it is the bitmask itself.

In your events, instead of passing in a bitmask, you can instead pass in the name of your bitmask

Help spread the word!

You may also like...

7 Responses

Personally I have taken a big interest in hemp and CBD products as an alternative medicine to manage various pains. It only really works though if you take a high enough dose. I like the CBD wax.. Its strong.. https://cbdfx.com/collections/cbd-oil/ Anyone try it before?

To create code blocks or other preformatted text, indent by four spaces:

This will be displayed in a monospaced font. The first four
spaces will be stripped off, but all other whitespace
will be preserved.
Markdown is turned off in code blocks:
[This is not a link](http://example.com)