Can someone explain Setbit()?

0 favourites
  • 3 posts
From the Asset Store
Give your players the chance to choose their own keys or buttons!
  • So I found Setbit and Getbit in system's expression list and couldn't really grasp the idea of how they work. Can someone explain how to use it properly and where to use it? .C3P would be really nice, also. Thanks!

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If you are not familiar with the binary system, look it up in google, there are tons of tutorials and videos.

    Any number is made of bits. For example, number 14 is 00001110 in binary. Setbit and Getbit allow to access individual bits in a number. Basically, this allows to use a single numerical variable as a small array of bits.

    For example, you might have 10 treasure chests in your game and you want to keep track of which chest is closed and which one is open. The usual way to do this is with an array. But you can also use a single global variable ChestsStatus to store this information for all 10 chests.

    When a chest becomes open, Set ChestsStatus to setbit(ChestsStatus, chestNumber, 1)

    This will set the bit for this chest to 1, which will indicate that this chest is open. Then you can save ChestsStatus variable to local storage, and later retrieve the information about open chests using getbit() expression.

  • Think of bits as light switches. They can be 0 or 1, off or on. Any set of bits can be represented by a single number. Changing any of the bits will change the number, so every arrangement of possible bits in the set will result in a unique number.

    Two use cases where utilizing setbit and getbit are common would be a bitwise auto tiling function for tilemaps and multiplayer client input states.

    A general case would be when the state of any single thing depends on the state of multiple other things. A single number variable to keep track of this utilizing bits can be convenient, but it's not the only way.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)