ObservedStack<T>

ObservedStack is a stack implementation that allows you to subscribe to changes made to the stack. When an item is added or removed from the stack, all subscribed callbacks are notified with the item and a ChangeType indicating whether the item was added or removed.

A stack is a linear data structure that allows for items to be added and removed in a last-in, first-out (LIFO) order. This means that the last item added to the stack will be the first one to be removed. Stacks are often used to store temporary data that needs to be accessed quickly or to reverse the order of items.

In game development, stacks can be useful for storing a player's undo actions, keeping track of visited locations in a game, or evaluating an expression. For example, a player may be able to undo their last move in a puzzle game by storing each move in a stack and popping the top item when the player wants to undo. Stacks can also be used to store items in a player's inventory, where the player can easily add or remove items from the top of the stack.

Usage

using Observed;

ObservedStack<int> stack = new ObservedStack<int>();
stack.Subscribe((item, changeType) =>
{
    if (changeType == ChangeType.Added)
    {
        Debug.Log($"{item} was added to the stack.");
    }
    else
    {
        Debug.Log($"{item} was removed from the stack.");
    }
});

stack.Push(1); // prints "1 was added to the stack."
stack.Push(2); // prints "2 was added to the stack."
stack.Pop(); // prints "2 was removed from the stack."

Last updated