BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

The object system

BF22142Heroes

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.

A con line, a template, an object con script ObjectTemplate.create Kit us_medic console object.method -> a registered handler ObjectTemplateManager type name -> class id; the active one a template (one per name) its class decides which property lines it takes components armor, fire, ammo, deviation, sound .. children (addTemplate) each with a position and a rotation property lines act on the ACTIVE template: the one last created or selected components are made by name: dice.hfe.world.TemplateComponent.<name> createObject the template's own factory objects in the world one tree per created template tree There is no RTTI: a class is an id and a registration string; an interface is a bare integer asked for by id. bf2_w32ded.exe!0x006ee3b0 (createTemplate), 0x005eefd0 (addTemplate), 0x005c3f60 (a component by name), 0x006f0070 (Object.create)
A script line reaches a handler through the console; the handler acts on the active template; a template creates objects.

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.Standard is class 0xc36f) 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 lineWhat happensWhere
ObjectTemplate.create <Type> <Name>createTemplateByTypeName: the type's class id, then createTemplate(class, name); the new template becomes the active onebf2_w32ded.exe!0x006edf90, 0x006ee3b0
ObjectTemplate.active <Name> / activeSafe <Type> <Name>selects an existing templatesetActiveTemplate 0x00569870
ObjectTemplate.<property> <args>the handler of that property, for the active template's classthe property tables (reference)
ObjectTemplate.addTemplate <Name>adds a child to a bundle0x005eefd0
ObjectTemplate.setPosition x/y/zplaces the child last added or selected0x004e7980
ObjectTemplate.createComponent <Name>adds a component0x005c3f60

Three things about these lines catch people:

  1. 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.
  2. 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.
  3. addTemplate of a name that starts with S_ 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):

text
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.con is 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 .con or .tweak line 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#

WhatWhere
the console and the con languagecrates/bf2-con
templates, their manager, the property tablescrates/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 propertiesre/data/template_types.tsv, re/data/template_properties.tsv
the world's objectscrates/bf2-world
Heroes' extra classescrates/bfh-data (classes)

Notes: notes/subsystems/object-model.md, notes/subsystems/object-templates.md. Next: the CON system, add a vehicle.

Source: docs/dev/engine/objects.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.