MontageGraph¶
Melee combat is one of the few genres where the animation is the gameplay. The swing you see is the swing that has to land, the recovery you see is the window you can be punished in, and the next attack in a combo has to blend out of exactly the pose you're standing in. Unreal gives you most of the pieces (montages, notifies, gameplay tags, the Gameplay Ability System), but wiring them together for EonBlade, an online sword-fighting game, kept running me into the same two problems:
- Which animation plays next? Attacks that chain into many other attacks, every link with its own blend settings, and designers who want to rearrange all of it without waiting on a programmer.
- Where is the blade, really? Hit detection that gives the same answer on the server and on the client, at 144 fps and at 20.
MontageGraph is the plugin I built to answer both. It is a graph editor for linked AnimMontage assets, plus a collision system that treats the animation as data to be baked rather than events to be waited for.
Source code on GitHub · Documentation · Sample project
The system is experimental and still under active development, so expect things to break.
Why not just use notifies?¶
The usual way to say "the blade is dangerous now" in Unreal is an AnimNotifyState on the montage, and for plenty of games it is all you need. But notifies are delivered by the animation tick, and the animation tick is only as steady as your frame rate; Vaei's LocoTips notes that they aren't guaranteed to trigger under variable frame rates. For a slasher, a late or skipped notify is not a cosmetic glitch. It is a hit that never happened, or one that happened on the server and not on the client.
MontageGraph takes the question "where is the weapon at time t?" out of the frame loop. The editor asks the animation once and stores the answer, and at runtime there is nothing to wait for, only a lookup by montage time. It is similar to the approach used by Digital Confectioners, and I'll get to how it works after the other half of the plugin: choosing the animation.
A graph of actions¶
A MontageGraph asset is a node graph:
- Montage nodes hold an
AnimMontageand everything authored on it (the sequences, collision windows, gameplay effects and events). They are the only nodes that carry anything playable. - Entry nodes are where a search starts: one per entry gameplay tag, such as a stance or a weapon style.
- Selector and Conduit nodes are pure routing.
The conditions live on the edges. An edge is one of:
| Edge | Taken when |
|---|---|
| Tag | one FGameplayTag is in the context. The common case: a stance, a combo step, an attack direction. It matches hierarchically (Direction.Forward.Right satisfies Direction.Forward) or exactly, as you choose. |
| Tag query | a compound FGameplayTagQuery passes, for what one tag can't say (a negation, an either/or). |
| Stack count | a gameplay effect's stack count reaches a threshold, for projects using the Gameplay Ability System. |
| (no condition) | never. See below. |
To pick an action, the game hands the graph a start node and a bag of tags and asks it to walk. At each node the first edge whose condition passes wins; edges are sorted by priority when the graph is baked, so "first" means "highest priority". The walk stops when no edge passes, when the winning edge loops back, or at a depth limit, and whatever node it ended on is what plays. This is the heart of it, simplified and reformatted from the plugin (the bounds checks and null guards are left out):
for (int32 e = Node.EdgeOffset, End = Node.EdgeOffset + Node.EdgeCount; e < End; ++e)
{
const FMGCompiledEdge& Edge = CompiledEdges[e];
bool bPass = false;
switch (Edge.Kind)
{
case EMGEdgeKind::Never:
break; // topology only, not a walkable transition
case EMGEdgeKind::Tag:
bPass = Edge.bExactMatch ? Tags.HasTagExact(Edge.MatchTag)
: Tags.HasTag(Edge.MatchTag);
break;
case EMGEdgeKind::TagQuery:
bPass = EdgeTagQueries[Edge.QueryIndex].Matches(Tags);
break;
case EMGEdgeKind::StackCount:
bPass = ASC->GetAggregatedStackCount(EdgeEffectQueries[Edge.QueryIndex])
>= Edge.RequiredStacks;
break;
}
if (bPass) { Next = Edge.TargetNode; break; }
}
Here is the same algorithm running in your browser, on a toy graph. Flip some tags and watch the walk resolve:
Context handed to the walk:
Swipe sideways to see the whole graph.
Two details of that graph are worth calling out.
The dashed edge. An edge with no condition isn't a transition at all, it's topology. It is how a combo chain is authored (Slash_Fwd into Slash_Fwd_2 and on), and the combat state machine steps along it positionally. The walk deliberately never takes one: if it did, a fresh button press would happily descend the whole chain and play the last montage in it.
The context belongs to the caller. The walk doesn't read the character's tags itself, it only sees the container it was handed. So a caller can correct the context first (drop a stale stance, substitute the flags of the attack it is about to commit to) and the walk can't quietly put the old ones back.
From objects to tables¶
Everything above is authoring. Editor nodes and edges are UObjects with virtual functions, which is what you want while building a graph and not what you want on the hot path of a fighting game. So when a graph is baked, it is compiled into flat tables:
- Each node becomes an
FMGCompiledNodeand each edge anFMGCompiledEdge: plain structs in arrays. A node's edges are a span (an offset and a count) into one shared edge array, in priority order, so a walk is a linear scan over contiguous memory with noUObjecttraversal. - The edge's subclass is baked into an enum (
EMGEdgeKind), so the walk needs noCast<>per edge. A tag edge stores its tag inline, which makes the common case a single container lookup. - A cooked build carries no node objects at all.
A node's runtime identity is its index into that table. That is cheap, but any change to the graph's shape renumbers every node, so each node also carries a GUID that survives re-bakes. When you re-bake a graph in the editor mid-session, the component that's playing it re-finds its live node through the GUID instead of silently pointing at a different attack.
The same care goes into streaming. A node's payloads (the montage, its collision bank, its effects) are soft references; the topology is not. A walk that could reach a node on one machine and not on another is a desync, not a hitch, so everything the walk reads stays resident and only the animations themselves stream. Each entry tag knows which packages its reachable nodes need (a branch), and a component holds claims on the branches it wants resident: the active one for whatever is equipped, plus any it always needs, like a guard. Switching the active branch requests the new one before releasing the old, so swapping between related styles doesn't evict and reload the nodes they share.
Baking the swing¶
The collision half of the plugin starts in the montage timeline. A collision window says when (a start and end time), where from (a bone or socket on the skeleton, called the mount), and which weapon bodies to sweep. When the graph is baked, the plugin evaluates the animation over the window, on the hero's own mesh so the proportions are the ones the game renders, and records the mount's pose: its location and rotation in the mesh's component space, with the root's own animation taken out. Root motion lives on the capsule at runtime, so composing a baked sample with the live mesh transform lands exactly where the rendered mount is. No bone is ever read at runtime, and every machine has the same samples.
Note what is baked: the path of the mount, not the shape of a weapon. I'll come back to shapes.
How densely to sample is the interesting part. Even spacing forces a bad choice between a huge bank and straight segments that cut corners on the fast part of a swing. So the sampler is adaptive. It seeds an even grid at 60 Hz, then compares the true pose at the midpoint of each interval with the straight-line interpolation (lerp the location, slerp the rotation). The error is measured at probe points 150 units out from the mount along each axis, the longest reach a weapon body is expected to have. If the worst probe lands more than 1 unit from where interpolation puts it, or consecutive samples rotate by more than 4°, the interval is split and checked again, down to 1/960 s. These bounds are editor settings, and the numbers above are the defaults.
A synthetic swing run through a Python port of the plugin's sampler. The bake is dense through the strike and sparse in the wind-up and recovery. Meeting the same two bounds with evenly spaced samples takes 360 Hz, more than twice as many.
At runtime, each tick the collision solver asks the component what montage time it has reached. For every window that has started and not finished, it sweeps from exactly where that window stopped last tick to exactly now: the interpolated pose at the start, every baked sample strictly in between, and the interpolated pose at the end. In the plugin it is just this (reformatted to fit):
// The polyline: the exact pose at From, every baked sample strictly between, the exact pose at To.
Points.Add(Bank->SampleAt(Seg, Range.From));
for (int32 k = UCollisionTracerBank::FindSampleAtOrBefore(Run, Range.From) + 1;
k < Run.Num() && Run[k].Time < Range.To; ++k)
{
if (Run[k].Time > Range.From)
{
Points.Add(Run[k]);
}
}
Points.Add(Bank->SampleAt(Seg, Range.To));
Because the sweep covers the whole span the animation advanced, rather than wherever the weapon happens to be on this frame, the answer stops depending on the frame rate:
- A 20 ms jab that opens and closes between two 30 Hz ticks is still swept in full.
- The clock is montage time, not accumulated wall time, so wind-ups that stretch, charge holds at rate 0 and charged releases that start mid-montage all behave. Nothing sweeps while the montage isn't moving forward, and a loop or section jump starts a fresh pass.
- That clock is overridable. A project that derives montage time from a deterministic timeline can make every machine sweep the same span, and a server doesn't need to tick the anim graph at all.
- The fighter's own movement is handled too. Samples are placed with the mesh transform interpolated to their own moment in the tick, so the early part of a lunge is swept from where the fighter was then.
Every baked sample is turned into a place in the world by one formula, shared by the live sweep, the editor preview, the clash volumes and the debug drawing, so none of them can disagree about where a weapon is:
BodyWorld(t) = Body · Posture · Mount(t) · ComponentToWorld(t)
Posture is whatever the fighter's stance does to the upper body that the montage can't know about (an aim correction, and a crouch under a layered slot). It is a pure function of replicated state.
To see why any of this matters, here is a toy experiment. A thin target sits in the path of the swing, and three ways of detecting the hit run against it at different frame rates, each at 120 different frame phases:
A toy model, not an engine capture: a synthetic swing, one thin target, and a Python port of the plugin's sweep. "Point" tests the blade where it is on each frame. "Frame chord" sweeps from last frame's position to this frame's, a common improvement. "Baked path" is the plugin's approach. The shape of the curves is the point.
Testing where the blade is on each frame is fine at 120 fps but already misses almost half the swings at 60, because at the fastest part of the swing the tip travels further between frames than the target is wide. Sweeping between frames holds up until the frame rate drops far enough for a chord to cut across the arc, which is exactly what a server running slower than its clients hands you. Sweeping the baked path doesn't care.
One solver, four stages¶
Every armed strike registers with a single world subsystem, UMGCollisionSubsystem, which ticks right after post-physics and evaluates all of them in stages:
- Prepare (game thread): each strike's effect windows, then the polyline each of its windows travels this tick, its bodies and its contact volumes. Pure description; nothing has hit anything yet.
- Query (parallel): every body gets one prefilter overlap over its region, then its steps are swept against the physics scene and against registered targets.
- Order: every hit and every strike-vs-strike contact is sorted by the moment inside the tick it happened.
- Dispatch (game thread): events are delivered in that order, and one whose strike was re-armed or disarmed by an earlier event is dropped.
The point of that shape is determinism: the verdicts depend only on what happened when, never on which fighter registered first. A blade that stopped on a wall at 0.3 of the tick never reaches the fighter it would have met at 0.6, and a clash at 0.2 pre-empts both blows it interrupted.
That clash is the one thing a world trace can't answer: two animating strikes meeting. The solver compares every pair of strikes that want contacts after they have all advanced, then hands each touching pair to the component with the lower unique id. What a contact means (a weapon clash, a parry, a ring-off) is up to the game.
Hundreds of fighters means hundreds of moving hit capsules, so instead of making every sweep pay the physics scene's broadphase for all of them, fighters can register their capsules as direct targets in a uniform grid rebuilt once a tick. The sweeps against them use the same narrowphase, so the answers are exact. stat MGCollision shows strikes, sweeps, prefilter rejects, volumes and contacts.
Bodies belong to the weapon¶
A montage's collision window says when and from where. It doesn't say what shape sweeps, because that belongs to whatever is in the fighter's hand. A project subclasses UMontageGraphComponent and overrides GetCollisionBodySet to supply the bodies the fighter currently holds, keyed by gameplay tag, in the mount's frame. A window's body tag is a query, matched hierarchically: Weapon.Body sweeps every weapon body, Weapon.Body.Head only the head. If nothing the fighter holds matches, the fighter's own bodies on that mount sweep instead (a fist, a foot).
So the same montage works for a dagger and for a greatsword, and swapping weapons never needs a re-bake. The weapon asset implements a small interface, IMGCollisionBodyProvider, so you can set it as a graph's Preview Body Provider and see its bodies on every window in the editor viewport, with a magenta flag on any window that names a body the weapon doesn't have. Contact metadata goes the other way: a ResolveCollisionTag override lets the project label which body hit, in its own vocabulary (a damage type, say).
Seeing what it does¶
The editor side matters as much as the runtime. The timeline is a custom dope sheet, loosely inspired by Cascadeur's, with tracks for animation segments, collision windows, gameplay effects and events, and montage sections. The fade timings in a link's default blend settings have drag handles, in the style of clip fades in a DAW, so I'm not typing numbers into a details panel.
Collision gets the same treatment. The viewport overlay and the in-game MontageGraph.ShowCollisionTracers draw through the same renderer in the same vocabulary, so a body looks identical in both. The one rule is that colour is state, never identity:
| Tone | Means |
|---|---|
| amber | live: an armed body, or a window the playhead is inside |
| crimson | hit, or a lost contact |
| cyan | the window being edited |
| magenta | the window names a body the preview weapon lacks |
| slate | not swept yet |
| green / violet | a contact won / mutual |
| teal | what the solver tested rather than swept (prefilter regions, contact spheres) |
Everything a sweep produces fades in world time, so Slomo 0.1 slows the fade along with the swing, and Slomo 0 freezes the whole picture on the frame where you stopped.
There is a smaller tool for bulk work too. Select some montage nodes and the Animation Modifiers panel collects every sequence under them, so you can add a native or Blueprint animation modifier to all of them, edit its settings in one place, apply it and save, without opening each asset. Modifiers are stored as Unreal's own asset user data, so they also show up in Persona.
Where it stands¶
MontageGraph is what EonBlade's combat runs on, and it is still moving quickly, which is why it wears the experimental warning. If you build melee combat in Unreal and any of this is useful, take a look at the source or the sample project, or find me on the Discord. Contributions, feedback and criticism are more than welcome, and if you're using it in a project of your own I'd love to add you to the showcase.