Formation
Class · USeinFormation : UObject
#include "Formations/SeinFormation.h"
abstract: use a subclass · can be subclassed in Blueprint · deterministic: safe in simulation code
Decides how a group of ordered units arranges itself on the ground when you give them a move order — the shape they spread into (a blob, a grid, a box, a wedge, a ring, a square) and which way they end up facing. This is the abstract base; pick a concrete shape, or subclass it in Blueprint to author your own.
Given an order target plus the set of members, a formation computes each member’s world position (index-aligned with the members) and the formation’s facing. It is the pluggable “how do N units arrange” seam, kept separate from dispatch (which ability runs, which member gets it — that stays on the command broker resolver). Formations are stateless pure compute: the framework runs them on per-world scratch instances initialized from class-default configuration, and rejects any reflected state written by an invocation. They are strictly deterministic — fixed-point math only, no float and no RNG — because the on-screen destination preview calls the exact same formation code the committed order does, and the two must agree bit-for-bit while lockstep networking must never desync.
The base ships a small footprint-aware toolkit shared by every shape: real-radius spacing that never overlaps (a big unit clears more room than a small one), 1-D line spreading, a size-graded cell-grid packer that places the biggest units front-and-centre, a de-overlap relaxation pass, and nav projection that folds any slot spilling off the play area back onto the nearest walkable cell. Framework subclasses are Formation (Blob) — everyone to the shared anchor, the default — plus the Grid, Box, Wedge, Ring and Square formations. Designers subclass for custom shapes and select one on the command broker resolver’s formation class / tag map.
Functions
Section titled “Functions”Compute Formation Facing
Section titled “Compute Formation Facing”Yaw-only facing that rotates the formation forward axis from CurrentCentroid toward TargetLocation (2-D, top-down — vertical ignored). Move-to-where-we- stand keeps CurrentFacing rather than degenerating to identity.
Pure compute, static — preview consumers (no instance) call it directly. Shared by every stock formation and the default resolver’s fallback path.
static FFixedQuaternion ComputeFormationFacing(FFixedVector CurrentCentroid, FFixedQuaternion CurrentFacing, FFixedVector TargetLocation);Facing From Direction
Section titled “Facing From Direction”Yaw-only facing that points the formation forward axis along DirectionXY (XY plane; Z ignored). Identity for a near-zero direction. The primitive behind ComputeFormationFacing; formations call it directly for facings that aren’t “toward the target” — e.g. a line facing perpendicular to itself.
static FFixedQuaternion FacingFromDirection(FFixedVector DirectionXY);Drag Facing Direction
Section titled “Drag Facing Direction”Facing DIRECTION (XY) for a right-click-drag: the guide line’s perpendicular on a fixed handedness derived from the drag DIRECTION (Start->End rotated a quarter turn). The drag is the authority: facing is independent of unit/centroid position, so a dragged formation faces by how the line was drawn, not where the units stand. Returns the zero vector when the guide isn’t a usable 2+ point line (caller keeps its non-drag facing). Feed the result to FacingFromDirection; drag the other way to flip the side.
static FFixedVector DragFacingDir(const TArray<FFixedVector>& GuidePoints);Project To Navigable
Section titled “Project To Navigable”Project a candidate slot to navigable ground (per-slot, occupancy-BLIND). On WALKABLE terrain the slot’s X/Y is kept exactly and only its Z is snapped to the ground (a formation keeps its top-down shape on a hill). A slot off the play area / on an obstacle is snapped to the NEAREST WALKABLE cell on the nav grid — independent of where the other slots land, so two slots can snap to the same edge cell here; the resolver’s batch ProjectPositionsToNavigable (occupancy-aware) + SeparatePositions passes are what guarantee the final layout is both on-nav and non-overlapping. Returns the raw Position when no nav is bound (tests / nav-less games), and Fallback only when there is no World at all. Deterministic — the nav grid is baked.
static FFixedVector ProjectToNavigable(USeinWorldSubsystem* World, FFixedVector Position, FFixedVector Fallback);Get Footprint Radius
Section titled “Get Footprint Radius”Footprint radius (world units) of an entity — the basis for footprint-aware formation spacing and preview dot sizing. Reads Extents Data: a Capsule shape contributes its Radius, a Box shape its circumscribed radius (√(hx²+hy²), orientation-independent so a rotating formation never overlaps), taking the MAX over all shapes (each pushed out by its LocalOffset XY). No extents / empty shapes → an ~infantry-sized fallback, so spacing degrades to uniform infantry spacing rather than collapsing to zero. Deterministic.
static FFixedPoint GetFootprintRadius(USeinWorldSubsystem* World, FSeinEntityHandle Handle);Gather Footprint Radii
Section titled “Gather Footprint Radii”Fill OutRadii (index-aligned with Members) with each member’s footprint radius via GetFootprintRadius.
static void GatherFootprintRadii(USeinWorldSubsystem* World, const TArray<FSeinEntityHandle>& Members, TArray<FFixedPoint>& OutRadii);Spread 1D
Section titled “Spread 1D”Centered 1-D placement that never overlaps footprints. Returns per-member signed offsets along an axis: adjacent centers are spaced max(MinGap, r[i]+r[i+1]); if TargetLength exceeds the tight span the slack is shared evenly across gaps (a long drag spreads the line without overlap), otherwise the tight no-overlap span is used. Centered on 0; index-aligned with Radii. Pass TargetLength = 0 for the pure no-overlap span.
static void Spread1D(const TArray<FFixedPoint>& Radii, FFixedPoint MinGap, FFixedPoint TargetLength, TArray<FFixedPoint>& OutOffsets);Sort Indices By Radius Desc
Section titled “Sort Indices By Radius Desc”Member indices sorted by footprint radius DESCENDING (largest first), tie-broken by index for determinism. Formations place big units first for clean size-graded shapes (wedge tip, box front-centre, grid embed).
static TArray<int32> SortIndicesByRadiusDesc(const TArray<FFixedPoint>& Radii);Pack Footprints
Section titled “Pack Footprints”Pack members (by footprint radius) into a tight cell grid for a clean size-graded block. Algorithm: the cell is the SMALLEST footprint diameter (floored at MinCell); each member becomes a ceil(diameter/cell)² block; the biggest blocks are placed front-and-centre (most- forward row, most-central free column there) and the 1×1s fill every remaining cell row-major. DesiredFrontWidth > 0 sets the front width in WORLD units (a drag draws it) — the grid is as many cells wide as span it; <= 0 → a square-ish grid (Columns = ceil(sqrt(total cells))). The column count is never narrower than the biggest box and is nudged so an even-span big box centres. Deterministic (fixed-point only). The shared packer behind the Grid (square) and Box (drag-width) formations; each applies its own world anchoring to the returned blocks.
static void PackFootprints(const TArray<FFixedPoint>& Radii, FFixedPoint DesiredFrontWidth, FFixedPoint MinCell, FSeinFootprintPacking& Out);Separate Positions
Section titled “Separate Positions”De-overlap / de-dup safety pass: push apart any two positions whose CENTRES are closer than the sum of their footprint radii (r_i + r_j) until none overlap — using each member’s ACTUAL footprint, so a big unit clears more room than a small one. Touching (centre distance == r_i + r_j) is left alone; only strict overlaps move. Coincident points separate along a deterministic index-derived direction. A bounded, deterministic relaxation (fixed pair order, fixed-point) so no formation ever returns two members on top of each other. Index-aligned with Radii.
static void SeparatePositions(const TArray<FFixedPoint>& Radii, TArray<FFixedVector>& Positions, int32 MaxIterations);Project Positions To Navigable
Section titled “Project Positions To Navigable”Final placement safety pass: any position that landed off the nav area, on an unrelated runtime blocker, or on top of a PARKED unit’s body is projected to the nearest free cell instead.
Free = walkable, clear of runtime nav blockers, not within footprint distance of any other slot, and not on a parked unit (an idle body that is not part of this order). Exclude From Occupancy lists this order’s own members, since they vacate their spots; their exact generational owner handles are also ignored as dynamic nav blockers, while unrelated blockers still apply. Every peer slot and parked body is treated as occupied during relocation so two overflowing slots never resolve onto the same spot. Positions already on free ground are left EXACTLY where they are (this never reshapes a clean formation). The shared net the resolver runs AFTER Separate Positions, so a formation spilling past the play-area edge or ordered onto a settled crowd packs onto the nearest open ground instead of grinding into bodies. Index-aligned with Radii; a missing radius counts as zero. Only PARKED units count as occupancy — moving traffic is transient and ignored (this also keeps the destination preview and the committed order in agreement: parked bodies are stable between the preview frame and the click). No-op when no nav is bound (tests / nav-less games). Deterministic — baked grid + start-of-tick collision snapshot.
static void ProjectPositionsToNavigable(USeinWorldSubsystem* World, const TArray<FFixedPoint>& Radii, TArray<FFixedVector>& Positions, const TArray<FSeinEntityHandle>& ExcludeFromOccupancy);Compute Member Facings
Section titled “Compute Member Facings”Fill per-member facings (index-aligned with Positions) per Mode: Uniform → all = UniformFacing; RadialOutward / RadialInward → each faces away from / toward Center (the formation anchor), falling back to UniformFacing for a member sitting on the centre. Computed by the resolver from the FINAL positions (after the layout passes), so radial directions are exact. Deterministic.
static void ComputeMemberFacings(ESeinFormationFacing Mode, const TArray<FFixedVector>& Positions, FFixedVector Center, FFixedQuaternion UniformFacing, TArray<FFixedQuaternion>& OutFacings);Events and overrides
Section titled “Events and overrides”Build Formation
Section titled “Build Formation”Overridable in Blueprint, with a native default
Lay out the given members for an order. Returns per-member world positions (index-aligned with Members) plus the formation’s facing at the target.
Pure compute: MUST NOT mutate sim state. Deterministic (fixed-point only) — the destination preview calls this exactly as commit dispatch does, so a non-deterministic override splits preview from commit AND desyncs lockstep.
Target carries the guide geometry (GuidePoints), the anchor, and the formation’s current centroid / facing (filled by the resolver). Default impl is a blob: every member to Target.Anchor, facing rotated centroid → anchor.
FSeinFormationLayout BuildFormation(USeinWorldSubsystem* World, const TArray<FSeinEntityHandle>& Members, const FSeinOrderTarget& Target);Properties
Section titled “Properties”Facing Mode
Section titled “Facing Mode”ESeinFormationFacing FacingMode · default ESeinFormationFacing::Uniform
Editable in class defaults · Blueprint read and write
Per-member facing policy (see ESeinFormationFacing). Default Uniform; Ring / Square default to RadialOutward via their constructors. Designer-overridable per formation.
C++ members (4)
static bool ExecuteStateless(USeinWorldSubsystem* World, const UClass* FormationClass, const TArray<FSeinEntityHandle>& Members, const FSeinOrderTarget& Target, FSeinFormationLayout& OutLayout, ESeinFormationFacing& OutFacingMode, FString* OutError = nullptr);Execute one formation through the framework’s stateless boundary. The CDO is configuration only. Runtime work happens on a per-world scratch instance, under read-only world access, and every reflected property must still match the CDO after the call. A violation fail-stops the world’s deterministic execution contract. Reentrant calls receive an isolated temporary instance, so nested preview/dispatch cannot observe partial state.bool IsStatelessExecutionAdmitted(FString& OutError) const;Native admission tripwire for the stateless execution boundary. The base admits Blueprints whose nearest native anchor isUSeinFormation. Every concrete native subclass must override and explicitly admit its own anchor; admission is never inherited accidentally by a later native subclass.static FSeinOrderTarget MakeInnerLayoutTarget(const FFixedVector& Anchor, const FFixedVector& Centroid, const FFixedQuaternion& Facing, const TSoftClassPtr<USeinFormation>& FormationClass);Build the order target for laying out a COMPOSITE element’s own contents (a group’s members) around the anchor the PARENT formation gave it. Carries the anchor, centroid and parent-given facing + the element’s own FormationClass — but DELIBERATELY no gesture guide and no formation tag: the parent formation already consumed the gesture to SPACE the element anchors, so the inner layout must keep its own compact shape (a guide here re-expands every element to fill the whole drag, overlapping them into one). The SINGLE constructor for inner-layout targets — used by a group’s resolver on commit AND by the preview — so the two can never drift and the gesture can never leak back into the inner layout. Top-level formations stay blind: they only ever see element count + footprint sizes.static FFixedVector ProjectToNavigableForMember(USeinWorldSubsystem* World, FSeinEntityHandle Member, FFixedVector Position, FFixedVector Fallback);Native member-aware projection preserves aerial destinations over ground blockers.