Libraries & Middleware
You built every algorithm in this course by hand so you know what's inside the box. In production, use the engine's systems first, then a well-known library, and only then your own code. This lesson is a map of what exists. Library features, licences and prices change, so check each project's current documentation and terms before you depend on it.
By engine
| Engine | Navigation | Decisions | Notes |
|---|---|---|---|
| Unity | AI Navigation package (NavMeshSurface, NavMeshAgent, links, obstacles) |
Behavior package (behavior graphs + blackboard) | Asset Store tools for behavior trees, FSMs and grid/graph pathfinding (for example the A* Pathfinding Project) cover what the packages don't. See Unity: AI Navigation & Behavior |
| Unreal | Navigation System with a Recast-based navmesh, navigation invokers for huge worlds | Behavior Trees, Blackboard, StateTree, EQS, AI Perception | Smart Objects (reservable interaction spots) and the Mass framework for large crowds. See Unreal: AIController, Behavior Trees & Blackboard |
| Godot | NavigationServer3D/2D, regions, agents, links, avoidance |
none built in | Add-ons from the Asset Library such as LimboAI (behavior trees + state machines) and Beehave (behavior trees in GDScript). See Godot: NavigationAgent3D |
| Web / JS | none | none | Yuka is a JavaScript game AI library (steering, state machines, goals, navmesh queries) that pairs well with Three.js |
Recast & Detour
Recast & Detour (the recastnavigation project, C++, permissive open-source licence) is the navmesh toolkit inside a large share of shipped games. Unreal's navmesh is built on it, and Godot uses Recast for baking.
- Recast builds the navmesh: voxelize the level's triangles, mark walkable voxels for an agent radius, height and climb, split them into regions, trace contours and output convex polygons (Navigation Meshes).
- Detour loads the result and answers queries: find the nearest polygon, find a polygon path with A*, then a straight path through the polygon portals (string pulling, as in Path Smoothing).
- DetourCrowd moves many agents at once with local avoidance; DetourTileCache handles obstacles that appear at runtime by rebuilding tiles.
If you write your own engine (see Game Engine Architecture in C++), integrating Recast & Detour is usually a better use of time than writing a navmesh generator.
BehaviorTree.CPP
BehaviorTree.CPP is a C++ behavior tree library popular in robotics and usable in games and servers. Trees are written in XML, so designers can edit them (its companion editor, Groot, has its own licence terms), and nodes exchange data through ports bound to a blackboard:
<root BTCPP_format="4">
<BehaviorTree ID="Guard">
<Fallback>
<Sequence>
<IsTargetVisible/>
<MoveTo goal="{target}"/>
</Sequence>
<Patrol/>
</Fallback>
</BehaviorTree>
</root>BT::BehaviorTreeFactory factory;
factory.registerNodeType<IsTargetVisible>("IsTargetVisible");
factory.registerNodeType<MoveTo>("MoveTo"); // a StatefulActionNode: onStart / onRunning / onHalted
factory.registerNodeType<Patrol>("Patrol");
auto tree = factory.createTreeFromText(xml);
tree.tickWhileRunning();Fallback is its name for a selector. The point to take away is the split: node types are code, tree shape is data. That split is also why engine behavior tree assets work: the same compiled nodes are reused in many trees. This model loads a tree from XML with a registry of node factories and runs it against two blackboards:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
const string xml = """
<BehaviorTree ID="Guard">
<Fallback>
<Sequence>
<IsSet key="target"/>
<Action name="MoveTo" key="target"/>
</Sequence>
<Sequence>
<IsSet key="noise"/>
<Action name="Investigate" key="noise"/>
</Sequence>
<Action name="Patrol"/>
</Fallback>
</BehaviorTree>
""";
// The registry: XML element name -> how to build that node.
var registry = new Dictionary<string, Func<XElement, Node>>();
Node Build(XElement e) => registry[e.Name.LocalName](e);
registry["BehaviorTree"] = e => Build(e.Elements().Single());
registry["Fallback"] = e => new Fallback(e.Elements().Select(Build).ToArray());
registry["Sequence"] = e => new Sequence(e.Elements().Select(Build).ToArray());
registry["IsSet"] = e => new IsSet((string)e.Attribute("key")!);
registry["Action"] = e => new ActionNode((string)e.Attribute("name")!, (string?)e.Attribute("key"));
Node tree = Build(XElement.Parse(xml));
var cases = new[]
{
new Dictionary<string, string> { ["noise"] = "crate at (4,2)" },
new Dictionary<string, string> { ["noise"] = "crate at (4,2)", ["target"] = "player" },
new Dictionary<string, string>(),
};
foreach (var bb in cases)
Console.WriteLine(quot;blackboard {{{string.Join(", ", bb.Select(kv => kv.Key + "=" + kv.Value))}}} -> {tree.Tick(bb)}");
abstract class Node { public abstract string Tick(Dictionary<string, string> bb); }
class Fallback(Node[] kids) : Node
{
public override string Tick(Dictionary<string, string> bb)
{
foreach (var k in kids) { var r = k.Tick(bb); if (r != "FAILURE") return r; }
return "FAILURE";
}
}
class Sequence(Node[] kids) : Node
{
public override string Tick(Dictionary<string, string> bb)
{
string last = "SUCCESS";
foreach (var k in kids) { last = k.Tick(bb); if (last == "FAILURE") return last; }
return last;
}
}
class IsSet(string key) : Node
{
public override string Tick(Dictionary<string, string> bb) => bb.ContainsKey(key) ? "SUCCESS" : "FAILURE";
}
class ActionNode(string name, string? key) : Node
{
public override string Tick(Dictionary<string, string> bb) =>
key == null ? quot;RUNNING {name}" : quot;RUNNING {name}({bb[key]})";
}blackboard {noise=crate at (4,2)} -> RUNNING Investigate(crate at (4,2))
blackboard {noise=crate at (4,2), target=player} -> RUNNING MoveTo(player)
blackboard {} -> RUNNING Patrol
Changing priorities (investigate before chasing?) is now an XML edit, with no recompile.
Choosing
- Use the engine's navigation. Writing a navmesh generator is a project in itself; tuning agent radius, step height and area costs is the real work.
- Pick the decision model for the game, then the tool. A handful of states: a code FSM. Many interruptible priorities: a behavior tree. Lots of options weighed against each other: Utility AI. Multi-step plans: GOAP.
- Check before you buy or adopt: which engine versions it supports, whether it's maintained, its licence and price terms, whether it works with your build targets (consoles, WebGL), and whether it has debugging tools. A library without a debugger costs more than it saves.
Test yourself
In Recast & Detour, which part builds the navmesh and which part answers path queries?
- Detour builds, Recast queries
- Recast builds (voxelize, regions, polygons), Detour queries (A* over polygons, straight path)
- Both build
- DetourCrowd builds
What is the benefit of defining behavior tree shape in data (XML or an asset) and node types in code?
- Trees run faster
- Designers can rearrange and reuse behaviors without recompiling
- It removes the need for a blackboard
- It makes trees deterministic