So I watched the brackeys object pooling video, and I’ve been messing around with the idea. In his example, he uses a for loop inside of a foreach, with the foreach looping through a list of class instances (aptly named pools) with each instance representing an amount of prefabs to instantiate. Each entry gets added to a dictionary as a Queue. The inner for loop represents the actual instantiation of each individual game object within the current Queue.
I remember seeing a video on reducing time-complexity in algorithms that are O(n^2) because of nested looping but I vaguely remember the implementation and even then, the example was in javascript and I’d have a hard time wrapping my head around it in the context of unity and C# gamedev. But I specifically remember the guy used some number of counter variables to separate the loops and iterate them sequentially rather than nested… or something like that lol
Anyways, I was wondering if anyone here had any ideas. Heres my current code:
using System.Collections.Generic;
using UnityEngine;
namespace Game
{
public class Pooler : MonoBehaviour
{
[SerializeField] List<Pool> pools;
Dictionary<EntityType, Queue<GameObject>> Registry;
void Awake()
{
Registry = new Dictionary<EntityType, Queue<GameObject>>();
Generate();
}
void Generate()
{
foreach (Pool pool in pools)
{
Queue<GameObject> currentQueue = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject currentObject = Instantiate(pool.prefab);
currentQueue.Enqueue(currentObject);
}
Registry.Add(pool.entityType, currentQueue);
}
print("POOLER: pools generated");
}
}
}
EDIT: EntityType is just an enum I created for a more verbose drop-down menu in the inspector. In brackeys’ example, he just uses a string and manually types each entry into the serialized field.