Data
Collections
Queue, Stack, Deque, PriorityQueue, RingBuffer, LRUCache, Set and Multiset with constant-time operations.
Getting it
local Collections = Engine:GetPackage("Collections")Overview
Plain Luau tables are great for arrays and dictionaries, but some jobs need a data structure with different guarantees: removing from the front of a list without shifting it, always getting the smallest item, or forgetting the oldest entries automatically. Collections provides those, each with a small consistent API.
All collections have a read-only Size and IsEmpty. Popping or peeking an empty collection raises an error, so check IsEmpty first when it might be empty.
local Collections = Engine:GetPackage("Collections")
local queue = Collections.Queue()
queue:Push("first")
queue:Push("second")
print(queue:Pop()) --> first
local recent = Collections.RingBuffer(10) -- keeps the last 10 entries
for i = 1, 25 do
recent:Push(i)
end
print(recent:Oldest(), recent:Newest()) --> 16 25Queue
Collections.Queue(items: { any }?): QueueFirst in, first out. Push adds to the back, Pop and Peek use the front.
| Member | Description |
|---|---|
:Push(value) | Add to the back. |
:Pop(): any | Remove and return the front item. |
:Peek(): any | Return the front item without removing it. |
:Clear() | Remove everything. |
:ToArray(): { any } | Items from front to back. |
Stack
Collections.Stack(items: { any }?): StackLast in, first out. Same members as Queue, but Pop and Peek use the most recently pushed item.
Deque
Collections.Deque(items: { any }?): DequeA double-ended queue: add and remove at both ends in constant time.
| Member | Description |
|---|---|
:PushFront(value), :PushBack(value) | Add at an end. |
:PopFront(), :PopBack() | Remove and return from an end. |
:PeekFront(), :PeekBack() | Read an end. |
:Clear(), :ToArray() | As above. |
PriorityQueue
Collections.PriorityQueue(less: ((a: number, b: number) -> boolean)?): PriorityQueueAlways hands out the item with the lowest priority number first (a binary heap). Pass your own less to change the order, for example function(a, b) return a > b end for highest first.
local jobs = Collections.PriorityQueue()
jobs:Push("save", 2)
jobs:Push("kick cheater", 1)
local job, priority = jobs:Pop() --> "kick cheater", 1| Member | Description |
|---|---|
:Push(value, priority: number) | Add an item. |
:Pop(): (any, number) | Remove and return the first item and its priority. |
:Peek(): (any, number) | Read the first item and its priority. |
:Clear() | Remove everything. |
RingBuffer
Collections.RingBuffer(capacity: number?): RingBufferA fixed-size buffer that overwrites its oldest item once full. Ideal for "the last N" of anything: chat lines, frame times, positions. Capacity defaults to the DefaultRingBufferCapacity setting.
| Member | Description |
|---|---|
Capacity: number, IsFull: boolean | Read-only. |
:Push(value) | Add an item, dropping the oldest if full. |
:Get(index): any | Item by age, 1 is the oldest. Errors when out of range. |
:Oldest(), :Newest() | The oldest and newest item. |
:Clear(), :ToArray() | ToArray returns oldest to newest. |
LRUCache
Collections.LRUCache(capacity: number?): LRUCacheA key-value cache that evicts the least recently used entry when it is full. Capacity defaults to the DefaultLRUCacheCapacity setting.
local thumbnails = Collections.LRUCache(200)
thumbnails.Evicted:Connect(function(userId, image)
print("dropped", userId)
end)
local function getThumbnail(userId)
local cached = thumbnails:Get(userId)
if cached then
return cached
end
local image = Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
thumbnails:Set(userId, image)
return image
end| Member | Description |
|---|---|
Capacity: number | Can be changed; shrinking it evicts entries. Must be at least 1. |
Evicted: Signal<(key, value)> | Fires when an entry is pushed out. |
:Get(key): any | The value, or nil. Marks the entry as recently used. |
:Peek(key): any | The value without touching its recency. |
:Set(key, value) | Insert or replace, marking it recently used. |
:Has(key): boolean, :Remove(key): boolean, :Clear() |
Set
Collections.Set(items: { any }?): SetA collection of unique items.
| Member | Description |
|---|---|
:Add(item): boolean | true if the item was new. |
:Remove(item): boolean | true if the item was there. |
:Has(item): boolean, :Clear(), :ToArray() | |
:Union(other), :Intersect(other), :Difference(other), :SymmetricDifference(other) | Return a new Set. |
:IsSubsetOf(other), :IsSupersetOf(other), :IsDisjointFrom(other) | Comparisons. |
Multiset
Collections.Multiset(items: { any }?): MultisetA set that counts how many of each item it holds, like an inventory of stackable items.
| Member | Description |
|---|---|
Size: number | Total count of all items. |
UniqueCount: number | Number of different items. |
:Add(item, amount?): number | Adds amount (default 1), returns the new count. |
:Remove(item, amount?): number | Removes amount (default 1), returns the new count. |
:Count(item): number, :Has(item): boolean | |
:ToArray() | Every item, repeated by its count. |
:UniqueItems() | Every different item once. |
Configuration
DefaultConfiguration/Packages/Collections2 settings| Setting | Default | What it does |
|---|---|---|
| DefaultRingBufferCapacity | 64 | Slots a RingBuffer.new() gets when no capacity is passed. |
| DefaultLRUCacheCapacity | 128 | Entries an LRUCache.new() holds when no capacity is passed. |