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

Add a weapon

We build mymod_dmr, a slower-firing, harder-hitting rifle based on Battlefield 2's M16A2, and give it to a kit. Every number here is a property you can look up in the reference.

BF22142Heroes

1. Anatomy of a hand-held weapon#

A hand-held weapon is a GenericFireArm template with components, plus a separate GenericProjectile template for what it shoots:

Component (createComponent)PrefixDecides
SingleFireComp (or MultiFireComp, LPFireComp, ThrownFireComp, ExplosivesFireComp)fire.rate of fire, fire modes, burst
DefaultAmmoCompammo.magazine size, number of magazines, reload time
SoldierDeviationCompdeviation.spread: base, per shot, moving, stance and zoom modifiers
SoldierBasedRecoilComprecoil.camera kick per shot
DefaultZoomCompzoom.zoom levels and delay
WeaponHudweaponHud.icons, HUD names, crosshair spread drawing
DefaultSoundComp, DefaultAnimationCompsound., animation.sounds and animation systems

The engine side of these components (deviation, heat, reload phases, damage falloff) is ported in crates/bf2-gameplay and checked against the original numbers.

2. The shipped M16A2#

USRIF_M16A2.tweak (shipped, the fire and ammo part)
ObjectTemplate.activeSafe GenericFireArm usrif_m16a2
ObjectTemplate.createComponent SingleFireComp
ObjectTemplate.fire.roundsPerMinute 900
ObjectTemplate.fire.addFireRate 1
ObjectTemplate.fire.addFireRate 0
ObjectTemplate.createComponent DefaultAmmoComp
ObjectTemplate.ammo.ammoType 1
ObjectTemplate.ammo.reloadTime 4.375
ObjectTemplate.createComponent SoldierDeviationComp
ObjectTemplate.deviation.setFireDev 2 0.2 0.05
ObjectTemplate.deviation.minDev 0.3
ObjectTemplate.deviation.setSpeedDev 1 0.2 0.2 0.1
ObjectTemplate.deviation.devModStand 1.5
ObjectTemplate.deviation.devModCrouch 1.3
ObjectTemplate.deviation.devModZoom 0.8

and at the end of the same file, the bullet:

USRIF_M16A2.tweak (shipped, the projectile)
ObjectTemplate.projectileTemplate M16a2Projectile
ObjectTemplate.velocity 1000
ObjectTemplate.itemIndex 3

ObjectTemplate.create GenericProjectile M16a2Projectile
ObjectTemplate.createComponent DefaultCollisionComp
ObjectTemplate.createComponent DefaultDetonationComp
ObjectTemplate.createComponent DefaultPenetrateComp
ObjectTemplate.physicsType Point
ObjectTemplate.collisionGroups 8
ObjectTemplate.material 38
ObjectTemplate.timeToLive CRD_NONE/0.5/0/0
ObjectTemplate.minDamage 1
ObjectTemplate.damage 30
  • fire.addFireRate lists the fire modes the player can switch through (0 single, 1 burst, 2 automatic); the first entry is the mode a fresh weapon starts in, and a weapon with none fires automatic.
  • deviation.setFireDev <max> <add per shot> <recovery> grows the spread with every shot up to max (notes/subsystems/gameplay-weapons.md: the fields fireDevMax, fireDevAdd, fireDevSub); minDev is the floor; devModStand / Crouch / Zoom multiply it. setTurnDev, setSpeedDev and setMiscDev follow the same max add ... sub order.
  • The projectile's damage against the target's material and the distance falloff decide a hit; timeToLive with velocity sets the range (0.5 s x 1000 m/s = 500 m).

3. Make the DMR#

Copy Weapons/Handheld/usrif_m16a2/ to Weapons/Handheld/mymod_dmr/, rename the files and the two template names (usrif_m16a2 -> mymod_dmr, M16a2Projectile -> mymod_dmr_projectile). Keep the mesh and sounds pointing at the M16's. Then change the numbers:

mymod_dmr.tweak
ObjectTemplate.activeSafe GenericFireArm mymod_dmr
ObjectTemplate.createComponent SingleFireComp
ObjectTemplate.fire.roundsPerMinute 300
ObjectTemplate.fire.addFireRate 0
ObjectTemplate.createComponent DefaultAmmoComp
ObjectTemplate.ammo.ammoType 1
ObjectTemplate.ammo.reloadTime 3.2
ObjectTemplate.createComponent SoldierDeviationComp
ObjectTemplate.deviation.setFireDev 4 0.5 0.4
ObjectTemplate.deviation.minDev 0.1
ObjectTemplate.deviation.devModZoom 0.4
ObjectTemplate.createComponent SoldierBasedRecoilComp
ObjectTemplate.recoil.hasRecoilForce 1
ObjectTemplate.recoil.recoilForceUp CRD_UNIFORM/0.8/1.2/0
ObjectTemplate.geometry USRIF_M16a2
ObjectTemplate.projectileTemplate mymod_dmr_projectile
ObjectTemplate.velocity 1200

ObjectTemplate.create GenericProjectile mymod_dmr_projectile
ObjectTemplate.createComponent DefaultCollisionComp
ObjectTemplate.createComponent DefaultDetonationComp
ObjectTemplate.createComponent DefaultPenetrateComp
ObjectTemplate.physicsType Point
ObjectTemplate.collisionGroups 8
ObjectTemplate.material 38
ObjectTemplate.timeToLive CRD_NONE/0.7/0/0
ObjectTemplate.damage 55

Semi-automatic only, a third of the rate of fire, more spread per shot (0.5 up to 4) that recovers faster, a bigger kick, tighter when zoomed, and a heavier, faster bullet with a longer range. Look up anything else in the GenericFireArm, SingleFireComp, SoldierDeviationComp and GenericProjectile references.

4. Give it to a kit#

Kits are templates too (Kit), listing their weapons with addTemplate. Copy the kit that carries the M16 (US_Assault in Kits/US/), rename it, and swap the weapon:

your kit's .tweak
ObjectTemplate.activeSafe Kit mymod_us_marksman
ObjectTemplate.addTemplate mymod_dmr

The level decides which kits a team can pick (gameLogic.setKit <team> <slot> <kit> <soldier> in the level's Init.con), so point a slot at your kit: gameLogic.setKit 2 1 "mymod_us_marksman" "us_light_soldier".

5. Check it#

  • fire.roundsPerMinute, ammo.* and the deviation are simulated on the server and predicted on the client: both must load the same files, or the client's spread will not match the hits.
  • Python can read what you set: obj.getTemplateProperty('damage') on a projectile or weapon object (host.Object).

In the other games#

  • 2142 adds heat (fire.heatAddWhenFire, coolDownPerSec, overheatPenalty), gun sway (sway.gunSway*) and unlock levels (unlockLevel); see the cross-game table.
  • Heroes weapons are the same components with cartoon tuning, plus abilities and hero items on top; its weapons are bought per hero, and their stats are scaled by the hero's level and gear.

How it is tested#

The lines this page uses are the shipped weapons' own, and those are held by the weapon tests (fire arms, deviation), which run the original's components on the shipped weapons' values. The example weapon itself is not built by a test: it is a recipe, checked by reading.

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-gameplaythe behaviour of gameplay objects: weapons, projectiles, soldiers, vehicles
Source: docs/dev/modding/add-weapon.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.