Working with Bit-switches

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

tut_bitSwitches1

This tutorial goes over a technique you can use with game variables to convert an event that might require several switches in order to implement.

Consider a puzzle consisting of four torches as shown above, where each torch controls a separate door, and different combinations of torches control different doors.

For example:

If torch 1 and 3 are ON, then door A will open.
If torch 1 and 4 are ON, then door B will open.
If torch 2 and 3 are ON, then door C will open.
If torch 1, 2, and 4 are ON, then door D will open.

One way to set up this puzzle is to use four switches, one for each torch. That is a very simple solution and gets the job done. However, what if you had 100 torches? Will you reserve 100 switches?

The solution I propose is to use Bit-Switches, which allow you to use a single variable to maintain all of these switches.

Required

Script: Bit Switches

Bit Switch Basics

Bit-switches work with binary numbers. Bit-switches have the following properties:

  • If the value is 0, then the switch is OFF
  • If the value is 1, then the switch is ON

tut_bitSwitches2

Bit-switches are read from right-to-left. If a bit is not shown, then it has a value of 0.

tut_bitSwitches3

Setting a bit switch

Suppose our variable initially has a value of 0. That means no torches are lit.

tut_bitSwitches4

Now we want to set bit-switch 3 ON. This can be done using the set_bit_switch script call:

tut_bitSwitches5

If you want to set multiple bit-switches, you can set each switch manually.
Alternatively, you can use a bit mask and use the set_mask_switch script call.

Suppose we want to turn on bits 2 and 4 using a bit mask:

tut_bitSwitches6

Note that in this case, a 1 means we will set that bit to true, and a 0 means we will ignore that bit. For example, to turn off a bit switch using masks:

tut_bitSwitches7

If you want to set ON and OFF bits at the same time, you can also use

set_mask_match(1, 0b1001, 0b0100)

The first mask will set the 1 bits to ON, while the second mask will set the 1 bits to OFF. So the script call above will turn ON bits 1 and 4, turn OFF bit 3, and ignore bit 2.

Checking Bit Switches

To check whether a single bit-switch is ON, use the bit_switch? script call:

tut_bitSwitches8

You can use a bitmask to check multiple bit switches as well. Again, only the 1 bits in your mask will be checked, while the 0 bits are ignored.

Use the mask_switch? script call to check multiple bits to see if they’re ON or OFF.

tut_bitSwitches9

Because you can only check for one type of value using a mask (ON or OFF, not both), you will need to perform multiple checks if you require certain bit switches to be ON, while certain bit switches are OFF.

For example, if we require switches 1 and 4 to be ON, but switch 3 must be OFF (and switch 2 doesn’t matter), you would say something like

mask_switch?(1, 0b1001, true) && mask_switch?(1, 0b0100, false)

Which first checks that bits 1 and 4 are ON, and then checks that bit 3 is OFF. Notice that bit 2 is completely ignored because we don’t care about that value.

However, it is possible to check whether bits are ON or OFF using a single script call as well:

mask_match?(1, 0b1001, 0b0100)

This is equivalent to the previous script call. The first mask checks for ON bits, while the second mask checks for OFF bits. Any bits that aren’t checked will be ignored.

Using Named Bitmasks

Sometimes you might be using the same bitmasks repeatedly in multiple places. Just think about how often you have the same switch condition in multiple events. If you decide to change your bitmask, then you would have to update every script call.

To make your projects more manageable, you can use “named bitmasks”, which is essentially associating a bitmask with a constant name that you can use in your script calls.

To create named bitmasks, go into the script’s configuration section and add new entries into the table. The keys are the bitmask names, while the value associated with a key is the bitmask.

tut_bitSwitches10

In your script calls, you can use one of the named bitmasks instead of using the bitmask itself:

tut_bitSwitches11

Conclusion

Bit switches may seem difficult to use at first, but once you have figured out how to set and check them, they should make complex events with many switches easier to manage since you can easily check multiple switches at the same time.

Spread the Word

If you liked the post and feel that others could benefit from it, consider tweeting it or sharing it on whichever social media channels that you use. You can also follow @HimeWorks on Twitter or like my Facebook page to get the latest updates or suggest topics that you would like to read about.

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

You may also like...

1 Response

  1. Rodjie says:

    This is legendary! you make the makers job easier.

Leave a Reply

Your email address will not be published.

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)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax