Working with Bit-switches

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.

You may also like...

22 Responses

  1. dobry sklep says:

    Hello there! Do you know if they make any plugins to help with Search Engine Optimization?
    I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good results. If you know of any
    please share. Kudos! You can read similar article here: E-commerce

  2. Hi there! Do you know if they make any plugins to help with Search Engine
    Optimization? I’m trying to get my blog to rank for some targeted keywords
    but I’m not seeing very good gains. If you know of any please share.
    Kudos! You can read similar blog here: Dobry sklep

  3. sklep online says:

    Wow, awesome weblog format! How lengthy have you been blogging for?

    you made blogging look easy. The whole look of your web site is wonderful, as smartly as the content material!

    You can see similar here sklep online

  4. Maram says:

    It’s a shame you don’t have a donate button! I’d most certainly donate to this superb
    blog! I guess for now i’ll settle for book-marking and adding your RSS
    feed to my Google account. I look forward to new updates and will share
    this blog with my Facebook group. Chat soon!

  5. Zoey says:

    I think this is one of the such a lot vital information for me.
    And i’m glad studying your article. But wanna statement on some basic issues, The website style is ideal, the articles is in point of fact nice :
    D. Good task, cheers

  6. Maleka says:

    Hello, I think your website might be having browser compatibility issues.

    When I look at your blog site in Ie, it looks fine
    but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, excellent blog!

  7. Lexie says:

    Nice post. I was checking constantly this blog and I am impressed!
    Very helpful info particularly the last part 🙂 I care for such info a lot.
    I was seeking this certain information for a long time.
    Thank you and good luck.

  8. Riko says:

    continuously i used to read smaller content which as well clear their motive, and that is
    also happening with this post which I am reading at
    this place.

  9. Sixto says:

    Fastidious answers in return of this matter with firm arguments and describing everything on the topic of that.

  10. Catarina says:

    It’s very simple to find out any matter on net as compared to books, as I found this paragraph at this web page.

  11. Kern says:

    I read this piece of writing completely about the resemblance of
    newest and earlier technologies, it’s awesome article.

  12. Symantha says:

    That is very interesting, You are an excessively professional blogger.
    I’ve joined your rss feed and sit up for in quest of more of
    your fantastic post. Additionally, I’ve shared your website in my social networks

  13. Simone says:

    If you desire to obtain a good deal from this post then you have to
    apply these methods to your won webpage.

  14. Rania says:

    I used to be recommended this website by my cousin. I am not sure
    whether or not this post is written by means of him as nobody else
    recognise such targeted about my trouble. You
    are incredible! Thank you!

  15. Mikio says:

    I think that what you wrote made a great deal of sense.
    But, what about this? suppose you were to write a killer headline?
    I ain’t suggesting your content is not good., but suppose you added something that makes people desire more?
    I mean Working with Bit-switches – 姫HimeWorks is kinda vanilla.

    You could look at Yahoo’s front page and note how they
    write post headlines to get people to open the links.

    You might add a related video or a related
    pic or two to grab people interested about what you’ve got to say.
    Just my opinion, it might make your website a little bit more interesting.

  16. Lionel says:

    It’s very simple to find out any topic on web as compared to books, as I found
    this article at this site.

  17. Gerron says:

    Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your website is excellent, as well as the content!

  18. Nicholaus says:

    A motivating discussion is definitely worth comment.
    I think that you ought to write more on this topic, it might not be
    a taboo subject but generally people do not talk
    about such subjects. To the next! Cheers!!

  19. Jeslyn says:

    Definitely consider that which you said. Your favourite justification appeared to be
    on the net the simplest thing to bear in mind
    of. I say to you, I definitely get irked whilst folks think
    about issues that they plainly don’t realize about. You managed to hit
    the nail upon the top as smartly as outlined out the whole thing without having side-effects , other people can take a signal.
    Will probably be again to get more. Thanks

  20. Kezia says:

    If some one wants to be updated with most up-to-date technologies
    after that he must be pay a visit this website and be up to date daily.

  21. Maximiliano says:

    Hey there superb website! Does running a blog like this take a great
    deal of work? I have no understanding of programming but I was hoping to start my own blog in the near future.

    Anyway, if you have any ideas or techniques for new blog owners please share.

    I know this is off subject nevertheless I just wanted to ask.
    Kudos!

  22. Rodjie says:

    This is legendary! you make the makers job easier.

Leave a Reply

Your email address will not be published. Required fields are marked *