InsanityPathfinder is a highly customizable but simple pathfinding class designed for advanced NPCs!
What does it exactly do?
The class provides you with customizable movesets and path generation, and some special events to use for building NPCs designed for fighting or parkour.
How do we use it?
Plop the module into ReplicatedStorage, then…
Creating and using pathAgent
as is:
local Insanity = require(Path.To.InsanityPathfinder)
local pathAgent = Insanity.new(Character)
pathAgent:CreatePath(TargetPosition)
pathAgent.Success:Connect(function()
pathAgent:start()
end)
Running custom path generation with a custom moveset:
local Insanity = require(Path.To.InsanityPathfinder)
local Moveset = {["RUN"]=function(Humanoid, End: Vector3)
if Success then
return true
else
return false
end
end)}
local PathGenerator = function(Path, Character, Target, Moveset, ErrorSignal)
return {Position, Move}
end
local PostChecker = function(Character, Waypoint)
return true
end
local pathAgent = Insanity.new(Character, Moveset, PathGenerator)
pathAgent:CreatePath(TargetPosition)
pathAgent.Success:Connect(function()
pathAgent:start()
end)
Customizing the Stuff!
Customizing Movesets
, PathGenerator
, and the PostChecker
Movesets
The moveset
is a table of character movement functions in which two functions are set in by default. Here is the two as an example:
moveset = {
["JUMP"] = function(Humanoid: Humanoid, End: Vector3)
local Success = nil
Humanoid:MoveTo(End)
Humanoid.Jump = true
local Success = Humanoid.MoveToFinished:Wait()
if Success then
return true
else
return false
end
end,
["WALK"] = function(Humanoid: Humanoid, End: Vector3)
local Success = nil
Humanoid:MoveTo(End)
local Success = Humanoid.MoveToFinished:Wait()
if Success then
return true
else
return false
end
end,
--[[ MOVES
it should always return a true or false, depending if the agent accomplished the move
]]
},
PathGenerator
The PathGenerator
is a function that handles all path generation. Here is an example from the defaults. Please note that the function’s arguments will be supplied by the pathAgent
.
DefaultGenerator = function(Path, Character, Target, Moveset, ErrorSig)
local Gen_Success = pcall(function()
-- _TypeCheck comes from an internal function
-- in the utilities module, please replace when
-- making your own generator
Path:ComputeAsync(_TypeCheck(Character), Target)
end)
if not Gen_Success then
ErrorSig:Fire()
return
end
local TempWaypoints = Path:GetWaypoints()
local ReturnNewWaypoint = {}
for Pos, Waypoint in ipairs(TempWaypoints) do
local NextWaypoint = TempWaypoints[Pos + 1] :: PathWaypoint
if not NextWaypoint then
continue
end
if (NextWaypoint.Position.Y - Waypoint.Position.Y) > 3 then
NextWaypoint = {Position=NextWaypoint.Position, Move=Moveset["JUMP"]}
else
NextWaypoint = {Position=NextWaypoint.Position, Move=Moveset["WALK"]}
end
table.insert(ReturnNewWaypoint, NextWaypoint)
end
return ReturnNewWaypoint
end
--[[ CHECKERS
1. the checker should always return a new table of waypoints.
2. the new table of waypoints shoud have their indexes added by 1
(it should ignore the first waypoint since it's just the character's position)
3. the new table of waypoints should contain tables in style of:
{Position: Vector3, Moveset: function}
]]
PostChecker
The PostChecker
is a function that acts like a sanity check that returns true if the character had actually accomplished the target of the move. Here is the default one as an example:
DefaultPostChecker = function(Character, Waypoint)
if (_TypeCheck(Character) - Waypoint.Position).Magnitude > 5 then
return false
end
return true
end
--[[ POST-CHECKERS
the post-checker acts as a sanity check after the move has been called and returned
the checker should return a true or false, depending if the its confirmed that the attempt suceeded.
]]
API Documentation
InsanityPathfinding.new(Character: Instance?, CustomMoveset: {string}?, CustomGenerator: string?, CustomPostChecker: string?, agentParameters: {string}?): pathAgent
The function used to make pathAgent
s.
Argument | Info |
---|---|
Character | The physical model of the agent. |
*CustomMoveset | A table of functions, able to be accessed by keys. |
*CustomGenerator | The path-generating function. |
*CustomPostChecker | The sanity check function. |
*agentParameters | The agentParameters |
Not Required *
pathAgent
The object that handles all the custom pathfinding stuff, all the event signals… and other stuff…
Setting | Purpose |
---|---|
Character | The physical model of the agent. |
Generator | The path-generating function. |
PostChecker | The sanity check function. |
Moveset | A table of functions, able to be accessed by keys. |
Event | Purpose |
---|---|
Error | Fires when the path generation fails. |
Success | Fires when the path generation succeeds. |
Blocked | Fires when the path gets blocked. |
Offcourse | Fires when the sanity check fails. |
TargetReached | Fires when the target has been reached. |
Function | Purpose |
---|---|
start() | Starts the waypoint following. |
stop() | Stops the waypoint following. |