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, aBitArray
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 aBitArray
instead of aList<bool>
could save a significant amount of memory.Bitwise operations: The
BitArray
class includes several methods for performing bitwise operations on its elements, such asAnd
,Or
, andXor
. 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:
You can then access and modify the array using the indexer just like you would with a BitArray
:
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:
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.
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 arrayUnsubscribe
: allows users to unsubscribe from change notificationsClearSubscriptions
: removes all subscriptionsSubscriberCount
: gets the number of subscribed callbacks
Last updated