PathfindingLib
Provides functionality for mod authors to run pathfinding off of the main thread.
About this mod Author’s original text
PathfindingLib
A library for Lethal Company mods (and probably mods for any other game using Unity's AI Navigation package) to run pathfinding off the main thread.
A high-level API is provided to allow convenient calculation of single paths off the main thread.
Synchronization functions are provided to prevent modifications to the navmesh while searching for a path on a non-main thread. If such a thread reads from the navmesh without synchronizing first, the engine may access invalid memory and crash. See the Threading Safety section.
This readme is a work in progress!
Feel free to submit suggestions for clarifications or improvements as issues or pull requests.
Usage
Note: This API is outdated. Documentation for the SmartPathfinding namespace will be added to the GitHub wiki later.
In order to run pathfinding off the main thread, the library provides a pre-built job called FindPathJob that can be used to calculate a path that the provided NavMeshAgent can traverse from a starting position to an ending position.
To do so, you will need to request a job from the pool of jobs:
var pooledJob = JobPools.GetFindPathJob();
Then you will need to initialize the job with the data it needs to run off the main thread:
pooledJob.Job.Initialize(origin, destination, agent);
If you are simply finding a path from the agent to a destination, it is recommended to use the GetPathOrigin() extension method to determine where such a path should begin, ensuring that the path succeeds even if the agent is on a navmesh link:
var origin = agent.GetPathOrigin();
pooledJob.Job.Initialize(origin, destination, agent);
After the job is initialized, you should schedule your job via ScheduleByRef() to run whenever there are job threads available, then store the job to check the status later. When scheduling, it is preferred to ensure that your job runs after any previous jobs you have scheduled for the agent:
var previousJobHandle = default(JobHandle);
for (var i = 0; i (pathNodesSize, Allocator.Temp);
query.GetPathResult(pathNodes);
// Release the lock once we are done with all code that needs access to our
// NavMeshQuery.
NavMeshLock.EndRead();
Credits
- Zaggy1024 - Initial development and maintenance
- mattymatty - Lots of general assistance, and the entire D* implementation used in the smart pathfinding
Install & use Install: BepInEx plugin
- The archive from playbuff is the mod itself — usually a BepInEx plugin (a .dll or a plugin folder once unzipped).
- Recommended: use a community mod manager (such as r2modman) — pick this game, import the mod, and the manager sets up BepInEx and its dependencies for you.
- Manual route: install the BepInEx loader in the game folder, then drop the unzipped .dll or plugin folder into BepInEx/plugins/.
- Launch the game — the BepInEx console logs every plugin it loads; seeing this mod in the log means it is installed.
Mods with listed dependencies need those installed too, or they will not load. Options are usually changed in-game or in the plugin's config file.
Install these first, or the mod will not work: