On this page
The object system
Everything in a level, from a soldier to a street lamp, is an object made from a template, and every template is made by lines of script. The engine has no fixed list of things: a mod adds a vehicle by writing a template. This page is how that works underneath; how to write the lines is on the CON system page.
Classes without names#
The engine's classes are registered with a class manager, one per process:
- a class is an id and a registration string (
dice.hfe.world.ObjectTemplateManager.Standardis class0xc36f) with a factory; - an interface is a bare integer; an object is asked for an interface by its id and answers with a pointer or nothing;
- a singleton is found by name (
"ObjectTemplateManager","SwiffPlayer"); - modules, static in the program and one array per library, feed classes and singletons into the manager.
There is no run-time type information in the programs. That shaped the whole port: every class in the Rust was found from its registration string, its factory and its table of functions, and a reader of the wiki sees interface ids such as 0xc4c5 where another engine would have a class name.
Templates#
IObjectTemplateManager keeps two tables: template types ("Kit" is class 0x9496), registered by the modules, and the templates themselves by name.
| Script line | What happens | Where |
|---|---|---|
ObjectTemplate.create <Type> <Name> | createTemplateByTypeName: the type's class id, then createTemplate(class, name); the new template becomes the active one | bf2_w32ded.exe!0x006edf90, 0x006ee3b0 |
ObjectTemplate.active <Name> / activeSafe <Type> <Name> | selects an existing template | setActiveTemplate 0x00569870 |
ObjectTemplate.<property> <args> | the handler of that property, for the active template's class | the property tables (reference) |
ObjectTemplate.addTemplate <Name> | adds a child to a bundle | 0x005eefd0 |
ObjectTemplate.setPosition x/y/z | places the child last added or selected | 0x004e7980 |
ObjectTemplate.createComponent <Name> | adds a component | 0x005c3f60 |
Three things about these lines catch people:
- Every property line acts on the active template, the one last created or selected. A line in the wrong place sets the wrong template, without an error when that template's class has the property too.
- A property exists per class. The same line is accepted by one template type and unknown to another; the generated property reference lists, per game, which class takes which line.
addTemplateof a name that starts withS_does not add a second child when one of that name exists: it selects it. That is how the automatic sound templates are tuned (the sound engine).
A just-created template runs a post-create hook before it is put in the table; some types use it to make their sound templates.
Components#
A fire arm is one template with many components: fire, ammo, deviation, sound, recoil, zoom, target, animation; a vehicle part has an armor component; a projectile has collision, detonation and follow components. A component is created by name: the engine builds dice.hfe.world.TemplateComponent.<name> and asks the class manager for it. Its property lines are written through the owner, as ObjectTemplate.fire.roundsPerMinute (weapons). Heroes adds component classes of its own (abilities).
Children#
A bundle keeps a list of children, each with a template name, a position and a rotation. A vehicle is such a tree: the hull, under it the engine, the wheels' springs, the seats, the weapons (engine, gearbox, wheels). At creation the tree of templates becomes a tree of objects.
Objects#
Object.create <template> [position] [rotation] (0x006f0070):
make the template active
obj = activeTemplate.createObject() each template class allocates its own object class
obj.setPosition(pos); set the rotation
objectManager.setActiveObject(obj) later Object.* lines act on it
make the object's template the active one- A level's
StaticObjects.conis thousands of such lines; spawners create objects during play. - A dedicated server creates nothing for a visual-only template: an effect bundle, a decal, an emitter, a sound template's object. The same level has fewer objects on a server than on a client, by design.
- An object's update runs once a tick in the objects' update (the frame and the tick); its state reaches clients through its networkables (ghosts).
Arguments are converted as the original converts them#
A script's 0.7 is text. The original reads it as decimal to double to float, like a C++ stream; integers and booleans have their own rules, and some properties convert seconds to ticks at once (weapons). The port's conversions are held bit for bit, because a template value that differs in its last bit is a simulation that differs.
How it is tested#
- Template scripts (
crates/bf2-objects/src/template/host.rs, 36 tests): a.conor.tweakline run through the port's command host stores what the original's setter stores: the Euler matrix in the original's float form, per-game constructor defaults, flags that propagate to children. - The object world (
crates/bf2-world/src/world.rs, 51 tests): creation from a template, the object cache, a message posted to a destroyed object that must not arrive, the absolute transformation of a child. - Per-class test programs (
crates/bf2-objects/tests): constructor values, a bundle's added templates, the rotational bundle's properties, and a run over the shipped levels' objects (retail_levels.rs, skipped without an install).
Few of this page's addresses stand in a test's own text: most of the object system is proven through what is built on it (a vehicle that drives, a weapon that fires) and counts as any, not strict, on parity.
In the code#
| What | Where |
|---|---|
| the console and the con language | crates/bf2-con |
| templates, their manager, the property tables | crates/bf2-objects/src/template/ (manager.rs, host.rs, loader.rs, props.rs); argument conversion in crates/bf2-objects/src/convert.rs |
| the generated tables of types and properties | re/data/template_types.tsv, re/data/template_properties.tsv |
| the world's objects | crates/bf2-world |
| Heroes' extra classes | crates/bfh-data (classes) |
Notes: notes/subsystems/object-model.md, notes/subsystems/object-templates.md. Next: the CON system, add a vehicle.
docs/dev/engine/objects.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.