You can give “tasks” to workers in actors that calculate the values, again it’s another rather niche scripting feature that requires extensive technical knowledge though.
Could you try some more basic things like reducing the number of iterations, and adding task.wait to prevent prolonged hanging?
i could reduce the number of iterations but when i use wait the lag disapears but the path generation becomes snails paced
Also ive tried parralel luau but never fully understood how to properly do it but im fine to learn it!
The pathfinding algorithm im using JPS is the most optimized one i chould find sense it maximizes bassicaly everything that a* is slow at plus i only use the cost from node to goal sense it makes the algorithm go straight for the goal and ignores accuracy
There’s a lot to unpack, but here are the two places I think might be hotspots for performance.
local RAY = workspace:Raycast(prev, D, Params)-- ray
if pcall(function() local a = RAY.Instance end) then
This could be replaced with a simple if RAY then. The closure being created here is probably a major bottleneck.
local function GCNgp(From:Vector3, PF_children:table)-- returns Grid_Location of a node from given world position
local temp, index, max = {}, 0, 9999
for _, child in ipairs(PF_children) do
local dist = (child.Position - From).Magnitude
if dist < max then
max = dist
index += 1
temp[index] = child
end
end
return temp[index]:GetAttribute("Grid_Location")
end
The temp table is useless here since you’ll always return the last value. Just keep a result variable and set it to next largest child in the loop.
local result, max = Children[1], 9999
for _, child in ipairs(PF_children) do
local dist = (child.Position - From).Magnitude
if dist > max then
max, result = dist, child
end
end
return result:GetAttribute("Grid_Location")
Attributes are especially slow from my experience. Look for alternatives, like a purely data based alternative
Using .Magnitude is slow because of sqrt. If you dont need the exact distance, there are alternatives that should still give mostly good results. This is something you should only be mindful of if its being done in bulk
The results are only so close to one-another because they compile to identical code. In the absence of an optimize flag, scripts ran in Studio are compiled with optimization level 1. If you ran them with 0 optimization, manually caching the function WOULD be significantly faster because the compiler wouldn’t be caching it for you.
Oh, okay, also
Holy moly, what a performance save!
Previously it needed 17 seconds of brutal lag to generate a path. But now
It does it in only 3 seconds of slight lag—nice!
If you are using A-star pathfinding, you should be able to recall it from finish to start instead of start to finish, that the target is the player and the player is the target. Also the longer the path is the lower performance you get