Skip to content

Instantly share code, notes, and snippets.

@ALLAH-2
Last active May 20, 2023 20:00
Show Gist options
  • Save ALLAH-2/892680966d46d15f743e4a7c08154ebb to your computer and use it in GitHub Desktop.
Save ALLAH-2/892680966d46d15f743e4a7c08154ebb to your computer and use it in GitHub Desktop.
codget lua pool
local RunService = game:GetService("RunService");
local Effects = Instance.new("Folder");
Effects.Name = "Effects";
Effects.Parent = workspace;
--[[
this is ap ool
assumes that everything inserted
has the same lifetime
if not then ur gonna make ap riority queue LOL[tweaker shit cuh]!
@author marcelo codget
@submodule Pool
]]
local Pool = {};
Pool.__index = Pool;
Pool.lifetime = 3; -- arb
Pool.capacity = 120; -- arb
Pool.readDestroy = 1;
Pool.read = 1;
-- purpose: you reuse element inside of the pool
function Pool.get(cframe, emit)
local object = Pool[Pool.read];
object.CFrame = cframe;
object.ParticleEmitter:Emit(emit);
-- handle the edge case where the read destroy
-- is the same as the read[needs to increment readDestroy]
local id = Pool.read + Pool.capacity;
if(Pool[id] ~= math.huge and Pool.readDestroy == Pool.read)then
Pool.readDestroy += 1;
if(Pool.capacity < Pool.readDestroy)then
Pool.readDestroy = 1;
end
end
-- append to pool
Pool[id] = os.clock() + Pool.lifetime;
Pool.read += 1;
if(Pool.capacity < Pool.read)then
Pool.read = 1;
end
end
-- purpose: called on heartbeat
local unused = CFrame.new(1e5,1e5,1e5);
function Pool.update()
local clock = os.clock();
local currentId = Pool.readDestroy + Pool.capacity;
-- check if the oldest object created can be deactivated
if(Pool[currentId] < clock)then
Pool[Pool.readDestroy].CFrame = unused;
Pool[currentId] = math.huge;
Pool.readDestroy += 1;
if(Pool.capacity < Pool.readDestroy)then
Pool.readDestroy = 1;
end
end
end
do
-- setup
local prefab = script.Parent.prefab;
for i = 1, Pool.capacity do
local clone = prefab:Clone();
clone.CanCollide = false;
clone.CanQuery = false;
clone.CanTouch = false;
clone.CFrame = unused;
clone.Parent = Effects;
Pool[i + Pool.capacity] = math.huge;
Pool[i] = clone;
end
RunService.Heartbeat:Connect(Pool.update);
end
return Pool;
@ALLAH-2
Copy link
Author

ALLAH-2 commented May 7, 2023

hi this is the best pool in existance cuz im the best and u r not Ok. Plz like and comment favoite and subscrib e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment