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.
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) | Prefix | Decides |
|---|---|---|
SingleFireComp (or MultiFireComp, LPFireComp, ThrownFireComp, ExplosivesFireComp) | fire. | rate of fire, fire modes, burst |
DefaultAmmoComp | ammo. | magazine size, number of magazines, reload time |
SoldierDeviationComp | deviation. | spread: base, per shot, moving, stance and zoom modifiers |
SoldierBasedRecoilComp | recoil. | camera kick per shot |
DefaultZoomComp | zoom. | zoom levels and delay |
WeaponHud | weaponHud. | icons, HUD names, crosshair spread drawing |
DefaultSoundComp, DefaultAnimationComp | sound., 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#
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.8and at the end of the same file, the bullet:
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 30fire.addFireRatelists 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 tomax(notes/subsystems/gameplay-weapons.md: the fieldsfireDevMax,fireDevAdd,fireDevSub);minDevis the floor;devModStand/Crouch/Zoommultiply it.setTurnDev,setSpeedDevandsetMiscDevfollow the samemax add ... suborder.- The projectile's
damageagainst the target's material and the distance falloff decide a hit;timeToLivewithvelocitysets 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:
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 55Semi-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:
ObjectTemplate.activeSafe Kit mymod_us_marksman
ObjectTemplate.addTemplate mymod_dmrThe 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):
| Crate | What it is |
|---|---|
crates/bf2-gameplay | the behaviour of gameplay objects: weapons, projectiles, soldiers, vehicles |
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.