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.
Files and where they live#
| Extension | What it holds | Typical place |
|---|---|---|
.con | creation scripts: ObjectTemplate.create, geometry and collision registration, level scripts | objects/.../<name>.con, levels/<map>/*.con |
.tweak | the tunable properties of templates (written by the original editor, edited by hand by modders) | next to the .con, pulled in by include <name>.tweak |
.inc | shared fragments (animation systems, includes) | objects/.../*.inc |
.ai | bot knowledge of a template | objects/.../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:
fileManager.mountArchive Objects_server.zip Objects
fileManager.mountArchive Menu_server.zip Menu
fileManager.mountArchive Common_server.zip Common
fileManager.mountArchive Booster_server.zip ObjectsThe 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
object.method arg1 arg2 ... rem a comment is a whole line starting with rem- The first word is the command:
ObjectTemplate.massmeans the methodmassof the console objectObjectTemplate. 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/zwithout spaces (0/0.5/-0.5), random distributionsCRD_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#
| Keyword | Meaning |
|---|---|
rem, beginRem / endRem | comment line, comment block |
if / elseIf / else / endIf | conditions (if v_arg1 == BF2Editor); compare with ==, !=, greaterThan, lessThan |
var v_name = value, const c_name = value | variables and constants; an argument starting with v_ / c_ is replaced by its value |
v_arg1 ... v_argN | the 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.tweak | runs a file in the current scope (how .con pulls in its .tweak) |
return | leaves 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.
ObjectTemplate.create PlayerControlObject usjep_hmmwv
ObjectTemplate.mass 1800
ObjectTemplate.geometry usjep_hmmwvObjectTemplate.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 followingObjectTemplate.<property>line sets a property on it.ObjectTemplate.activeSafe <Type> <name>makes an existing template active again (that is how.tweakfiles start). It does nothing if the template does not exist, which is why it is called safe.Object.create <template>thenObject.absolutePosition x/y/zplaces an instance (levels do this).
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:
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#
- The mod's archives are mounted (
ServerArchives.con/ClientArchives.con). - The game runs
objects/.../<name>.conwhen something needs the template: a level'sStaticObjects.conruns the static objects it uses, the game mode'sGamePlayObjects.connames the vehicles its spawners create, kits name their weapons. The.conregisters the mesh (GeometryTemplate.create BundledMesh <name>) and collision (CollisionManager.createTemplate <name>), creates the template and ends withinclude <name>.tweak. - The
.tweakre-activates the template (activeSafe) and sets everything that makes it play the way it does.
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#
| Piece | Code |
|---|---|
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, defaults | crates/bf2-objects/src/template/ (props.rs is generated by re/py/gen_template_props.py from re/data/template_properties.tsv) |
| 2142 additions | crates/bf2-objects/src/template/props_2142.rs, extra_2142.rs |
| Heroes additions | crates/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
objectTemplatecommands. - 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.setKittakes 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,runwith its arguments and its result,includesharing 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
.conand.tweakfiles; 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):
| Crate | What it is |
|---|---|
crates/bf2-con | the console script language: lexer, interpreter, command registry |
crates/bf2-objects | the object model: class and interface ids, templates, objects |
crates/bfh-data | Battlefield 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.
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.