So i was trying to make an entity system (client rendered) for my td game since my current one uses align on client which isn’t ideal for performance. I’m not really a good programmer so this code isn’t the best:
So my current system stats with only position calculation (not updating any model cframe) : 21~26ms on average for 20k enemies
My current current systel stats with moving models (updating cframe) : 36~45ms in average with only 2k enemies
I hope my calculation part isn’t too bad but once i try to update the enemy models cframe my performance just becomes really worse and everything i tried couldn’t change anything about it /:
I already sacrified a lot of readability to get even the smallest (micro) optimizations i could get
those micro optimzations had a really great effect on the calculation part which allowed me to double my performances, but on the cframe updating part i am unable to even optimize the script, so help would be really useful
NOTE: this is also not really good since i’m mainly only calculating the position , no rotation with it
and i’m using native luau
--!native
local replicatedStorage = game:GetService("ReplicatedStorage")
local replicatedFirst = game:GetService("ReplicatedFirst")
local modules = replicatedStorage.Modules
local clientModules = replicatedFirst.ClientModules
local enemyContainer = require(modules.EnemyContainer)
local playerSettings = require(clientModules.PlayerSettings)
local enemyHandler = {}
local frameCount = 0
function enemyHandler:CalculatePos(enemy)
local nodesFolder = workspace.Nodes
local currentTime = workspace:GetServerTimeNow()
local moveFrom, moveTo, before, spawnTime = enemy.Before, enemy.MoveTo, enemy.Before, enemy.SpawnTime
local enemyTimes = enemy.nodeTimes
while enemyTimes[moveTo] < currentTime - spawnTime do
before, moveTo = moveTo, moveTo + 1
end
local startPos, endPos = nodesFolder[before].Position, nodesFolder[moveTo].Position
local timeToFinish, timeLeft = enemyTimes[moveTo] - enemyTimes[before], enemyTimes[moveTo] - (currentTime - spawnTime)
local alpha = (timeToFinish - timeLeft) / timeToFinish
enemy.Before, enemy.MoveTo = before, moveTo
return CFrame.new(startPos:Lerp(endPos, alpha)) * enemy.RootPart.CFrame.Rotation
end
function enemyHandler.UpdatePos()
frameCount = frameCount + 1
if frameCount <= playerSettings.RefreshRate then
return
end
frameCount = 0
local enemyTable = {}
local posTable = {}
for id, enemy in pairs(enemyContainer) do
local rootPart = enemy.RootPart
local timeElapsed = workspace:GetServerTimeNow() - enemy.SpawnTime
if timeElapsed >= enemy.FinishTime then
if rootPart.Parent then
rootPart.Parent:Destroy()
end
table.remove(enemyContainer, id)
else
local cframe = enemyHandler:CalculatePos(enemy)
table.insert(enemyTable, enemy.RootPart)
table.insert(posTable, cframe)
end
end
workspace:BulkMoveTo(enemyTable, posTable)
end
return enemyHandler
Specs:
CPU Ryzen 5 5600H
GPU Rtx 3050
Ram 16gb