Skip to main content

Writing Deterministic Code

The simulation must give the same result for the same inputs on every machine and every build. Everything the online match, the rollback, the sync test and the replays rely on comes from this rule. Follow it whenever you write a frame event, a state condition or change the simulation code (AFTCore).

The rules​

  1. Only FAFTSimState changes during Step. It must stay POD without padding: AFT_ASSERT_ROLLBACK_SAFE checks it at compile time (std::is_trivially_copyable and std::has_unique_object_representations). Use int32 fields, FAFTFixed, fixed size arrays; no pointers, no TArray, no bool between int32s (padding).
  2. No floats, no FMath::Rand, no time, no world queries. Use FAFTFixed math and the simulation random generator (State.Match.Rng).
  3. Data comes from FAFTSimDatabase (compiled at battle start, read only). A new asset value that affects gameplay must be added to the content hash (HashValue), so that two peers with different data refuse to play.
  4. Stable iteration order: no TMap / TSet iteration in the simulation (lookups are fine).
  5. Effects go through events (Emit), played by the presentation; never spawn or play anything from the simulation.
  6. Run a Sync Test match after every gameplay change (see Testing the Netcode).

Fixed capacities​

AFTCoreTypes.h defines the capacities of the state (no allocation per frame):

ConstantValue
FrameRate60
MaxTeams / MaxTeamSize2 / 3 (6 fighters)
MaxProjectiles16
InputHistoryLength32 frames per team
MaxHitboxes / MaxHurtboxes8 / 6 per entity
MaxHitsPerAction8
MaxCancelActions12
MaxGotoChain4

Fixed point​

FAFTFixed stores a value in 1/1000 of a centimeter in an int32 (AFT::FixedScale = 1000). In the editor, fixed values are shown and edited in centimeters (a details customization converts them). FAFTFixedVector and FAFTFixedBox are the vector and box types of the simulation; UAFTFixedLibrary exposes them to Blueprint: conversions for tools and UI, and the deterministic math nodes (+ - * /, comparisons, Fixed From Int...) of the Blueprint extensions below.

Where to extend​

ExtensionHow
New frame eventDerive from FAFTFrameEvent, implement Execute (see Actions and Frame Events). Or in Blueprint: parent class AFT Frame Event Blueprint, override Execute with the AFT Frame Event functions and the AFT Fixed math nodes (see Custom frame events (Blueprint)).
New state graph conditionDerive from FAFTStateCondition, implement Evaluate. Or in Blueprint: parent class AFT State Condition Blueprint, override Evaluate with the AFT State Condition functions (see Custom conditions (Blueprint)).
Wall rulesDerive from UAFTWallRules, override NativeWallContact / NativeHit / NativeComboEnd and HashSettings; state in the fighter and match wall memory only. Or in Blueprint: override On Wall Contact / On Hit / On Combo End with the wall context (see Walls).
CPU brainDerive from UAFTCpuBrain, override Think; random numbers and memory from the context only. Or in Blueprint: override On Think with the CPU context (see Custom CPU Brains).
PresentationAnything in AFTGame reads the state and the events: fighter actor, camera, presentation, HUD can use floats and the world freely.