ObservedQueue<T>

ObservedQueue is an extension of the C# Queue class that allows you to subscribe to changes made to the queue. This can be useful in certain cases where you want to be notified of when items are added or removed from the queue.

A queue is a linear data structure in which elements are added at one end (the tail) and removed from the other end (the head). This is known as a First-In, First-Out (FIFO) structure because the element that has been in the queue the longest is the first one to be removed.

Queues are often used in game development to store and process events or tasks. For example, a queue could be used to store user input events that need to be processed in the order they were received. This ensures that events are handled in the correct order, rather than possibly being missed or processed out of order if they were handled individually.

Additionally, queues can be used to store and process AI actions or animations in a game. This allows the game to control the order in which these actions are taken, rather than having them all happen at once.

Overall, queues are a useful data structure for maintaining order and processing elements in a specific sequence in game development.

Usage

Here is an example of how you might use this class:

using Observed;
using UnityEngine;

public class MyMonoBehaviour : MonoBehaviour
{
    private ObservedQueue<int> queue;

    private void Start()
    {
        // Create a new ObservedQueue<int>
        queue = new ObservedQueue<int>();

        // Subscribe to changes made to the queue
        queue.Subscribe((item, changeType) =>
        {
            if (changeType == ChangeType.Added)
            {
                Debug.Log($"Item {item} was added to the queue.");
            }
            else if (changeType == ChangeType.Removed)
            {
                Debug.Log($"Item {item} was removed from the queue.");
            }
            else if (changeType == ChangeType.Updated)
            {
                Debug.Log($"Item {item} was updated in the queue.");
            }
        });

        // Add an item to the queue
        queue.Enqueue(1);
        // Output: Item 1 was added to the queue.

        // Remove an item from the queue
        queue.Dequeue();
        // Output: Item 1 was removed from the queue.
    }
}

Properties

  • int Count: Gets the number of elements contained in the ObservedQueue<T>.

  • bool IsReadOnly: Gets a value indicating whether the ObservedQueue<T> is read-only.

Methods

  • void Subscribe(Action<T, ChangeType> callback): Subscribes to changes made to the queue. The callback function will be called with the item that was added or removed, and a ChangeType enum value indicating whether the item was added, removed, or updated.

  • void Unsubscribe(Action<T, ChangeType> callback): Unsubscribes from changes made to the queue.

  • void Clear(): Removes all objects from the ObservedQueue<T>.

  • void ClearSubscriptions(): Removes all subscriptions to changes made to the queue.

  • void Enqueue(T item): Adds an object to the end of the ObservedQueue<T>.

  • T Dequeue(): Removes and returns the object at the beginning of the ObservedQueue<T>.

  • T Peek(): Returns the object at the beginning of the ObservedQueue<T> without removing it.

Last updated