Main menu:

Last Updated: January 17th, 2016

Quick Links:

Concept

One *global* bitmask (Completed_Levels) represents which levels are fully completed while the other *object oriented *bitmask (Level_Requirements) specifies the prerequisites for a given level. If the expression "Completed_Levels AND Level_Requirements = Level_Requirments" is true, then the object with the Level_Requirements is accessible. A Level's Level_ID is an integer ranging from 0 - 31. When a level is completed, the *nth* bit (Level_ID) of the Completed_Levels bitmask flips on.

**Saving Completed Levels**

After a level is completed, we need to update Completed_Levels to reflect the current state of completed levels. This modification is performed by modifying Completed_Levels with the following expression: "Completed_Levels = Completed_Levels OR Level_ID".

For those familiar with basic graph theory, the way some items must be played/accessed before other items resembles a *directed acyclic graph*. As such you can apply some graph algorithms, such as topological sorting, on the item set.

Introduction Material: The Itty Bitty Bits of Numbers

With respect to electronics, numbers are composed entirely of 0s (**off/low/false**) and 1s (**on/high/true**) called *bits*. As such, numbers are expressed in *binary*. MMFusion, integers are represented with 32 bits with the 0th bit as the *least significant bit* and the 31st bit as the *most significant bit*.

**Note**

Usually, bits are divided into groups of four for visual clarity.

**Examples Decimal Integers Expressed as 32-bit Binary Numbers**:

1 = 0000 0000 0000 0000 0000 0000 0000 0001 = 1 (simplified form)

2 = 0000 0000 0000 0000 0000 0000 0000 0010 = 10 (simplified form)

100 = 0000 0000 0000 0000 0000 0000 0110 0100 = 11 0100 (simplified form)

777 = 0000 0000 0000 0000 0000 0011 0000 1001 = 11 0000 1001 (simplified form)

-1 = 1111 1111 1111 1111 1111 1111 1111 1111 (simplified form)

This mechanic simply requires knowledge that MMFusion expresses integers with 32 bits, though additional useful topics which you can research are listed below.

**Additional Topics**:

- Two's Compliment (Expressing Negative Numbers in Binary)
- Converting Decimal Numbers to Binary

Introduction Material: Bitwise Operators

More information about bitwise operators and Expression Editor functions may be viewed at the Expression Editor Basics tutorial.

Introduction Material: Bitmasks

In short, bitmasks are a data type similar to strings and integers. Bitmasks are typically composed of a certain number of bits, such as 32 or 64. As such, numerical data types, such as integers, can act as a bitmask. Actions, conditions, and expressions involving bitmasks compare or act on individual bits, rather than the numerical value itself. In MMFusion, Object Flags constitute a basic bitmask, though manipulating multiple flags is lengthy and gruesome. As such, this tutorial uses Global Values and Alterable Values as bitmasks.

The Importance of the AND and OR Operators

Prerequisites: [0000 0000] (0) (no prerequisites)

Level_ID: [0000 0001] (1) (used if any level needs A as a prerequisite)

Prerequisites: [0000 0001] (1) (A as a prerequisite)Level_ID: [0000 0010] (2) (used if any level needs B as a prerequisite)

Prerequisites: [0000 0011] (3) (A and B as a prerequisite)Level_ID: [0000 0100] (4) (used if any level needs C as a prerequisite)

Also suppose that, initially, the number of Completed_Levels variable is the following: *Completed_Levels*: [0000 0000] (**meaning no levels are initially completed**)

========= ========= ========= (AND Operation)

0000 0000 0000 0000 0000 0000 (if the level is accessible, this should match the level's Level_ID)

If Completed_Levels was [1100 1011] (**203**), all three levels would be accessible as shown below.

0000 0000 0000 0001 0000 0011 (if the level is accessible, this should match the level's Level_ID)

Bitmasks & Level Selections

By now, we know electronics express numbers in binary form through the use of 1s and 0s, also called **bits**. A continuous set of bits constitute a **bitmask**, which in can be expressed through numerical data types such as *integers*. The bits that compose a bitmask have specific operations associated with them; the AND operation turns bits off (*to 0*), the OR operation turns bits on (*to 1*), and the NOT operation inverses all bits in a bitmask. How does this information allow us to create a level selection mechanic?

As mentioned in the Concept, three bitmasks are maintained; global variable Completed_Levels, object variable Level_Requirements, and object variable Level_ID.

- Completed_Levels represents the state of up to 32 unique levels. Each bit represents the completion status of one level. A level is considered unfinished if its corresponding bit is 0 and finished if its corresponding bit is 1. As levels are completed, the
**nth bit**of Completed_Levels is turned on through the OR operator.*(based off of Level_ID)*

- Level_ID represents the level's corresponding bit (0 - 31) in Completed_Levels variable. When levels are completed, their Level_ID is ORed with the Completed Levels, effectively unlocking more levels.

- Level_Requirements represents the absolute minimal requirements before a specific level is accessible.
**A given level is considered accessible if the evaluates true: Completed_Levels AND Level_Requirements = Level_Requirements.**

Summary

There is nothing more to this mechanic outside of the bitmasks and bitmask operators. There are three aspects which might generate issues.

- Improperly or incorrectly setting up the required values
- Improper use of the AND/OR operators
- Incorrect object scoping to an individual level.

MMFusion builds a 32-bit application so all numerical values are limited to 32 bits. As such, you will only be able to express 32 levels per bitmask. You will need to use and maintain multiple sets of Completed_Level, Level_ID, and Level_Requirement variables or use Alterable Strings if you wanted more than 32 levels.

You can also usetopological sortingif you can arrange the set of data into a. All nodes which have no inbound nodes are possible options for the user to select.directed acyclic graph

Rate This Tutorial

Vote: 2.5/5

If you have read this tutorial, rate it from **poor** (1) to **average** (3) to **great** (5)!