ObservedBitArray

ObservedBitArray is a class that extends the functionality of the built-in BitArray class by allowing users to subscribe to changes made to the array. This can be useful in scenarios where multiple components or objects need to be notified when the array is modified.

A BitArray is a collection of bits (values of either 0 or 1) that can be manipulated as a unit. It is useful for storing and manipulating small amounts of data that is easy to represent as a series of bits.

There are several ways that a BitArray can be useful in game development. Here are a few examples:

  • Storing flags: A BitArray can be used to store a series of flags, where each flag is represented by a single bit. For example, a BitArray could be used to store a set of boolean values, such as whether a certain game object is visible, whether it is collidable, or whether it has been selected by the player.

  • Optimizing memory usage: Because a BitArray uses a smaller amount of memory than other collections, it can be useful for optimizing memory usage in games. For example, if a game needs to store a large number of boolean values, using a BitArray instead of a List<bool> could save a significant amount of memory.

  • Bitwise operations: The BitArray class includes several methods for performing bitwise operations on its elements, such as And, Or, and Xor. These can be useful for optimizing certain types of calculations in games.

Usage

To use ObservedBitArray, you can instantiate it in the same way as you would a BitArray, with either an integer length or a boolean array as a parameter:

var observedBits = new ObservedBitArray(5);
// or
var observedBits = new ObservedBitArray(new bool[] { true, false, false, true });

You can then access and modify the array using the indexer just like you would with a BitArray:

observedBits[0] = true;
observedBits[1] = false;

var firstBit = observedBits[0]; // true

To subscribe to changes made to the array, use the Subscribe method and pass it a callback function that takes in an integer index and a BitArrayChangeType enumeration value:

observedBits.Subscribe((index, changeType) =>
{
    switch (changeType)
    {
        case BitArrayChangeType.SetTrue:
            Console.WriteLine($"Bit at index {index} was set to true.");
            break;
        case BitArrayChangeType.SetFalse:
            Console.WriteLine($"Bit at index {index} was set to false.");
            break;
    }
});

observedBits[2] = true; // "Bit at index 2 was set to true."
observedBits[3] = false; // "Bit at index 3 was set to false."

You can unsubscribe from change notifications by using the Unsubscribe method and passing in the callback function that you used to subscribe. To clear all subscriptions, use the ClearSubscriptions method.

observedBits.Unsubscribe((index, changeType) =>
{
    switch (changeType)
    {
        case BitArrayChangeType.SetTrue:
            Console.WriteLine($"Bit at index {index} was set to true.");
            break;
        case BitArrayChangeType.SetFalse:
            Console.WriteLine($"Bit at index {index} was set to false.");
            break;
    }
});

// or
observedBits.ClearSubscriptions();

Additional Members

ObservedBitArray also includes the following members in addition to those inherited from BitArray:

  • Subscribe: allows users to subscribe to changes made to the array

  • Unsubscribe: allows users to unsubscribe from change notifications

  • ClearSubscriptions: removes all subscriptions

  • SubscriberCount: gets the number of subscribed callbacks

Last updated