I’ve created an NPC hivemind system that uses pathfinding to keep NPCs from clumping together when fighting a player.
How the system works:
-Closest NPC aggressively attacks player
-Other NPCs “hover” around the player, through the use of pathfinding. The system was designed for them to avoid a certain radius around the player if possible.
EXPECTED BEHAVIOR:
As you can see in this video, the NPCs are successfully pathfinding to random points around the player.
PROBLEM:
The NPCs will suddenly all stop pathfinding and stand completely still. This seems to happen at complete random.
I have narrowed this behavior down to ComputeAsync calls slowing down drastically, and at random, sometimes taking up to 45 seconds, when most of the time it takes < .2 seconds. The result is that all pathfinding-related processes are paused due to ComputeAsync’s long yield time and the NPCs stand still. This seemingly happens to all of the NPCs in the game at once.
local StartTime = tick()
local success, errorMessage = pcall(function()
path:ComputeAsync(CharacterModel.PrimaryPart.Position - offset, destination)
end)
local TotalTime = tick() - StartTime
print("Path Compute Time: "..tostring(TotalTime))
Above is a less extreme example, but it’s still extremely odd that ComputeAsync yields for up to 3 seconds in this case.
THESE ARE NOT THE CAUSES:
-Memory leakage. I have confirmed that there is no memory leakage relating to Navigation
-Networkownership. I have confirmed that networkownership is always set to nil for NPCs
-Logic errors in my code. I have confirmed that the NPCs are attempting to pathfind when they should, it’s just taking a very long time thanks to ComputeAsync
POTENTIAL CAUSES:
Are there limits to the number of consecutive ComputeAsync calls you can make? Does it start to throttle if there are too many? I make calls very frequently so this explanation seems plausible, and I’m taking measures to lessen the amount of ComputeAsync calls.
Any other ideas? Beyond that, I’m completely stumped. Thank you for your help.