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

The CON system

Every object in the three games (a rifle, a jeep, a flag, a whole level) is built by plain-text console scripts. Learn this language and you can change almost anything without touching code.

BF22142Heroes

Files and where they live#

ExtensionWhat it holdsTypical place
.concreation scripts: ObjectTemplate.create, geometry and collision registration, level scriptsobjects/.../<name>.con, levels/<map>/*.con
.tweakthe tunable properties of templates (written by the original editor, edited by hand by modders)next to the .con, pulled in by include <name>.tweak
.incshared fragments (animation systems, includes)objects/.../*.inc
.aibot knowledge of a templateobjects/.../ai/Objects.ai

The files sit in zip archives that a mod mounts by name. BF2's own mod does it in mods/bf2/ServerArchives.con and ClientArchives.con:

mods/bf2/ServerArchives.con (shipped)
fileManager.mountArchive Objects_server.zip Objects
fileManager.mountArchive Menu_server.zip Menu
fileManager.mountArchive Common_server.zip Common
fileManager.mountArchive Booster_server.zip Objects

The server reads *_server.zip (logic: templates, physics, game modes); the client also reads *_client.zip (meshes, textures, sounds, effects). A template must exist on both sides with the same name, because the network refers to templates by name and id.

The line model#

A CON file has no syntax tree: the interpreter executes it line by line against a stack of scopes (Console::executeLine, bf2_w32ded.exe!0x006517a0; in bf2-rs crates/bf2-con). Each line is

con
object.method arg1 arg2 ...        rem a comment is a whole line starting with rem
  • The first word is the command: ObjectTemplate.mass means the method mass of the console object ObjectTemplate. Case does not matter for commands or names.
  • Arguments are split on spaces and tabs; "quotes" group words; there are no escape sequences; at most 13 arguments.
  • Vectors are written x/y/z without spaces (0/0.5/-0.5), random distributions CRD_UNIFORM/min/max/0.
  • Errors in script files are reported nowhere and execution continues with the next line. A typo silently does nothing, so check your property names against the property reference.

Keywords#

KeywordMeaning
rem, beginRem / endRemcomment line, comment block
if / elseIf / else / endIfconditions (if v_arg1 == BF2Editor); compare with ==, !=, greaterThan, lessThan
var v_name = value, const c_name = valuevariables and constants; an argument starting with v_ / c_ is replaced by its value
v_arg1 ... v_argNthe arguments a run passed to the script
run file.con [args]runs another script in a new scope (it sees the caller's variables)
include file.tweakruns a file in the current scope (how .con pulls in its .tweak)
returnleaves the current script

while loops do not exist in the shipped engine (the code is dead), and > after a space redirects output to a file, so write comparisons with greaterThan rather than >. The complete rules, with every quirk, are in notes/formats/con-scripts.md.

Templates and objects#

The core idea of Refractor 2: templates describe a kind of thing; objects are instances placed in the world. What the files a template names contain (meshes, textures, materials, sounds) and a real template tree drawn from the editor are on What goes into a game asset and What goes into an object.

create, then set properties on the active template
ObjectTemplate.create PlayerControlObject usjep_hmmwv
ObjectTemplate.mass 1800
ObjectTemplate.geometry usjep_hmmwv
  • ObjectTemplate.create <Type> <name> makes a new template of a class (PlayerControlObject, GenericFireArm, GenericProjectile, Engine, Spring, SpawnPoint, ... see the classes) and makes it the active template. Every following ObjectTemplate.<property> line sets a property on it.
  • ObjectTemplate.activeSafe <Type> <name> makes an existing template active again (that is how .tweak files start). It does nothing if the template does not exist, which is why it is called safe.
  • Object.create <template> then Object.absolutePosition x/y/z places an instance (levels do this).
PlayerControlObject usjep_hmmwv ObjectTemplate.create / activeSafe Components armor.hitPoints 1000 vehicleHud.hudName ... radio.spottedMessage ... Own properties mass 1800 geometry USJEP_HMMWV physicsType Mesh Children (addTemplate + setPosition / setRotation) USJEP_HMMWV_Steeringwheel RotationalBundle USJEP_HMMWV_CupolaBase PlayerControlObject (gunner) Drivetrain_RF/LF/LR/RR, Engine, EntryPoints, Sounds ... Engine template torque, gears, input Spring + wheel suspension, grip, rotation Geometry + collision .bundledmesh / .collisionmesh
A vehicle is a tree of templates: the root carries components and its own properties; children are other templates placed with setPosition / setRotation.

Children#

ObjectTemplate.addTemplate <child> attaches another template under the active one; the next ObjectTemplate.setPosition / setRotation lines place that child relative to its parent. A vehicle's wheels, engine, seats, turret, sounds and effects are all children. When the parent is created in the world, the whole tree is instantiated.

Components#

Many behaviours are components: sub-objects a template creates with ObjectTemplate.createComponent <Kind>. Their properties carry a prefix:

components of the HMMWV (shipped USJEP_HMMWV.tweak)
ObjectTemplate.createComponent Armor
ObjectTemplate.armor.maxHitPoints 1000
ObjectTemplate.armor.hitPoints 1000
ObjectTemplate.armor.hpLostWhileUpSideDown 125
ObjectTemplate.createComponent VehicleHud
ObjectTemplate.vehicleHud.hudName "KILLMESSAGE_VEHICLE_hmmwv"

Weapons are almost entirely components: fire. (SingleFireComp, MultiFireComp, ...), ammo., deviation., recoil., zoom., sound., animation.. The property reference lists every component and its properties with defaults.

How a template is loaded#

  1. The mod's archives are mounted (ServerArchives.con / ClientArchives.con).
  2. The game runs objects/.../<name>.con when something needs the template: a level's StaticObjects.con runs the static objects it uses, the game mode's GamePlayObjects.con names the vehicles its spawners create, kits name their weapons. The .con registers the mesh (GeometryTemplate.create BundledMesh <name>) and collision (CollisionManager.createTemplate <name>), creates the template and ends with include <name>.tweak.
  3. The .tweak re-activates the template (activeSafe) and sets everything that makes it play the way it does.
Copy, rename, tweak

Nearly every new object starts as a copy of a shipped one: copy the folder, rename the files and every template name inside them, then change properties. Template names are global, so a copy that keeps an original name overwrites the original.

Where this lives in bf2-rs#

PieceCode
The interpreter (lines, keywords, variables, run / include)crates/bf2-con (console.rs, lexer.rs, script.rs)
The command registry (every object.method with its argument types and access)crates/bf2-con/src/registry.rs, re/data/console_commands.tsv (BF2), re/data/bf2142/console_commands.tsv, re/data/bfheroes/console_commands_126.tsv
Template classes, properties, defaultscrates/bf2-objects/src/template/ (props.rs is generated by re/py/gen_template_props.py from re/data/template_properties.tsv)
2142 additionscrates/bf2-objects/src/template/props_2142.rs, extra_2142.rs
Heroes additionscrates/bfh-data/src/templates.rs, console_bfh.rs

The whole retail script corpus (11,527 BF2 scripts) parses and runs through bf2-con; the property tables are read from the original binaries, so the defaults you see in the reference are the original constructors' values.

Per game#

  • Battlefield 2 is the baseline: about 1,200 objectTemplate commands.
  • Battlefield 2142 keeps the language and adds properties (pods, Titan parts, gun sway, EMP) and template classes; a few commands changed their argument lists (gameLogic.setKit takes one more string).
  • Battlefield Heroes has the largest table (abilities, hero items, cartoon effects) and removes some military features. See the cross-game command table.

Next: add a vehicle and add a weapon.

How it is tested#

  • The language, from the decompiled interpreter (crates/bf2-con/tests/language.rs, 40 tests): comments, if / elseIf / else, run with its arguments and its result, include sharing the caller's scope, each written from the original's code.
  • When an object is active (active_checks_bf2.rs): the client's console objects answer only with the engine objects their listing reads.
  • Templates (crates/bf2-objects/tests): what a template's constructor leaves in it against the listing (ctor_values_bf2.rs), the value converters against the original's run natively (conversions.rs).
  • Every shipped script: the level and object tests run the game's own .con and .tweak files; a line the port cannot read fails there.

In the code#

The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):

CrateWhat it is
crates/bf2-conthe console script language: lexer, interpreter, command registry
crates/bf2-objectsthe object model: class and interface ids, templates, objects
crates/bfh-dataBattlefield Heroes data: the template properties and console objects it adds

Files this page names: crates/bf2-con/src/registry.rs, crates/bf2-objects/src/template/props_2142.rs, crates/bfh-data/src/templates.rs, crates/bf2-con/tests/language.rs.

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