FERRETGame engine over Babylon.js
Ferret

Building a Ferret game for Unreal Engine

A finished Ferret game can be built into a native Unreal Engine 5.8 project and a packaged Windows game (.exe). The project is one a person keeps working on in the UE editor: levels are .umap maps with ordinary actors, models are Blueprint actor classes, the HUD is UMG widgets, sounds are sound assets. The goal is that a game converts with a minimum of edits in UE; for the four test games (wabi-sabi, falcon, airwar, gearbox) with none.

What is carried over are Ferret's mechanics, not Babylon. There is no browser in the game: no embedded Chromium, no emulated Babylon scene graph, no DOM engine.

What happens to each part#

A Ferret game is its own code (a few hundred to a few thousand lines in the test games) on top of the @ferret/* packages (90-98 % of the code it runs). Each module of each package is on one of two sides:

SideWhatIn the UE build
translatelogic: the ECS core, flight dynamics, vessels and hull resistance, gearbox and drivetrain, AI behaviours, character movement, interaction rules, physics capabilities over the kernel, wave maths, networking records, synth presets, the MP3 and WAV codecstranslated to C++ with the game's own code, so the numbers are the browser's own
uedrawing, materials, lights, particles, sky, water surface, landscape, audio output, the HUD, input and XR devices, asset loading, the physics backendgiven by the FerretRuntime plugin on UE's own system, behind the same API

Some of the ue counterparts: level packages become .umap maps; the environment becomes Sky Atmosphere, Directional and Sky Light, Exponential Height Fog and a post-process volume; landscapes become UE Landscape; clouds Volumetric Clouds; ambient particles and swarms Niagara; sequences Level Sequences; the physics kernel (packages/physics/src/kernel.ts) runs on Chaos instead of Havok; navigation uses UE's navigation mesh; sounds become sound waves and the synth recipes MetaSound sources; input goes through Enhanced Input; XR through OpenXR; the network wire through UE's WebSocket and HTTP modules.

The split is one file, tools/ue-cpp/boundary.json: every module the games reach, its side, and for a ue module the UE system that gives it.

How a ue module is bound. The translator never emits C++ for a ue module. Where translated code imports one, the import becomes a lookup in a table of module objects (uemod(key)), and the FerretRuntime plugin fills that table at start-up: for each export it calls ueExport(module, name, value) with the C++ class, function or constant that implements it on a UE system. An import whose export the plugin did not register is collected in a list and reported at start-up (ueMissingList), so a missing piece is a named error at the first run, not a silent undefined. A translate module is emitted with the game and needs nothing from the plugin.

The Babylon facade#

In the browser the translated code still speaks Babylon: the game code moves a TransformNode, builds a sphere with MeshBuilder, sets a StandardMaterial's colour, adds two Vector3s. In UE that code reaches Babylon only through the facade: the closed list of Babylon classes and members in tools/ue-cpp/facade.json. Each class has a kind and a UE counterpart:

KindExamplesIn UE
valueVector3, Quaternion, Matrix, Color3, BoundingInfo, Observablevalue types with Babylon's maths, in Ferret's frame (converted to UE's (-x, z, y) * 100 where a value is handed to UE)
sceneTransformNode, Mesh, Camera, Light, StandardMaterial, PBRMaterial, DynamicTexture, SolidParticleSystem, Scenea UE object: a scene component, a static or procedural mesh component, a camera component, a light component, a material instance of the plugin's master materials, a canvas render target, an instanced mesh component, the game's world
constantAxis, Constants, VertexBuffer kindsconstants mapped where they are used

Value types are TypeScript. Vector3, Quaternion, Matrix, Color3 and the rest of the value kind are written in tools/ue-cpp/facade/math.ts, line by line after Babylon 9.x (a Matrix keeps its 16 numbers in a Float32Array as Babylon does, so a product rounds exactly where Babylon rounds). The file is translated to C++ with the game like any other code, so there is no hand-written C++ copy to drift. Conformance case 020-babylon-math runs this file and Babylon itself on the same inputs and requires the same output; adding a member to a value type means adding it to math.ts and to that case.

The four test games reach 62 classes and about 330 members through the facade. tools/ue-cpp/facade.d.ts is generated from the list and Babylon 9.33.1's typings: each member with its exact declaration, the API that FerretRuntime implements.

Writing a game that builds for UE. Use the @ferret/* APIs for what they cover; use Babylon directly only within the facade. A Babylon member outside it makes the build fail with its place. When a game needs it, either express it through a Ferret API (the browser build keeps working), or add it to the facade with its UE counterpart and implement it in FerretRuntime. Nothing is stubbed.

Commands#

node tools/ue-cpp/facade.mjs check  examples/falcon            # boundary + facade: fails naming each module or member not covered, with file:line
node tools/ue-cpp/facade.mjs update examples/falcon            # adds what the game reaches to facade.json (a new class needs its UE counterpart before check passes)
node tools/ue-cpp/facade.mjs dts                               # regenerates facade.d.ts after facade.json changed
node tools/ue-cpp/survey.mjs examples/falcon                   # everything the game reaches: constructs, Babylon, Web APIs, shaders, assets
node tools/ue-cpp/cli.mjs build examples/falcon [--config Shipping] [--out <folder>] [--engine 5.8]

From the editor, Caroline's ue_build runs the same build in the importer app on the person's computer and puts the game zip and the project zip in Cat Space (the importer app). The generated project, the content import and the packaging options are on the generated UE project.

Errors#

The build never skips silently. It stops and lists every case of:

  • a module of @ferret/* that is not in boundary.json, or a ue module without its UE counterpart;
  • a Babylon class or member the translated code reaches that is not in the facade, or a facade class without a UE counterpart;
  • a TypeScript construct the translator refuses (eval, new Function, with);
  • an asset or level the code loads that cannot be resolved to a file, or that the import cannot bring into UE.

The fix is to build the missing piece (the translator, the plugin, the facade) or to move the game onto a Ferret API; never to patch the generated project.

The build queue#

One computer runs one UnrealBuildTool / AutomationTool at a time. Every compile, package and plugin step of tools/ue-cpp/project/package.mjs takes a turn in a first-come queue (~/.ferret/ue-build-queue, FERRET_BUILDQ_DIR), shows who is ahead in its log, and starts the stuck watch only when it really runs. A script that calls Build.bat or RunUAT.bat itself wraps the call: node tools/ue-cpp/project/buildq.mjs run --label <what> -- <command> <args>; node tools/ue-cpp/project/buildq.mjs status shows the queue. FERRET_BUILDQ=0 switches it off.

Working on the plugin: write everything, build once#

A UE build is slow (compile, import, cook, package: an hour or more on a busy machine) and UBT and UAT run one at a time per computer. So a change is not tried by building after every fix. First write all of it and check it with cheap means: one TypeScript test file, the native MSVC conformance run of the changed tests (tools/ue-cpp/test/conformance), and the UE 5.8 headers (C:\Epic Games\UE_5.8\Engine\Source) for every API name. Then build once and verify the games in parallel from that build. Build the game target, not only the editor: it fails on what the editor build lets through.

Traps found in UE 5.8 (each cost a build):

  • Editor-only members do not exist in a packaged game: UTexture::MipGenSettings, UMetaSoundBuilderBase::Build(Options) (use Build(nullptr, Options)); wrap in #if WITH_EDITORONLY_DATA or use the call both builds have.
  • Python in the editor lacks some names: unreal.MaterialExpressionCameraVector (use CameraVectorWS), MP_CustomizedUVs* (the property enum is hidden), and PerInstanceCustomData takes const_default_value. A material that does not compile under SM6 is replaced by the default material in the game, silently; look for Failed to compile Material in the cook log.
  • UStereoLayerComponent::StereoLayerType is protected (its default is face-locked); EHMDTrackingOrigin::Eye is Local.
  • UAT BuildCookRun needs -WaitMutex in -UbtArgs, else a second build on the computer fails with ExitCode=10.
  • Chaos: a particle without a unique ParticleID collides with nothing (pairs are keyed by it); the default material sleeps a body after one frame (set the sleep thresholds); CCD on every body asserts in the mid-phase; gravity is in cm/s^2; FCylinder is not supported in shape casts.

Limits#

At most 8 CPU cores for every run (UBT, shader workers, tests). The version stamp of a build is the local time of the building computer, yyyymmddHHMM, shown on the game's splash. The plan of the work is plans/PLAN-UE-BUILD.md.