How to create a Destroyable Building in Battlefield: 1942

 

 
Updated: October 16th 2003

Download this MiniMod (unzip into the BattleField 1942\Mods folder)

A great dynamic feature to add to a mod is a destroyable building.  A destroyable building adds realism to the game and gives the player the impression that they are tangibly affecting the game.  This tutorial will show you how to create your own destroyable building.

 

STEP 1 – Creating the Models

 

 The first step to building a new destroyable building is to create the models of the building.  There will need to be three new models created to bring this new object to life.  The first model will be the low polygon model that will be seen by players while they are far away from the building.  The second model will be the detailed model of the building as it will appear when a player is either close to the building or inside of it.  The third model will be the destroyed version of the building.  This model will be seen only after the building has been destroyed. 

The main mesh should be exported with a collision mesh.  The collision mesh will need to have a material ID assigned to it for the building to be destroyable.  You can learn more about material ID’s in the Damage System Tutorial. 

For the purposes of this tutorial, we have provided a mini-mod that also contains the three demo models.

 STEP 2 – Creating the Geometry Objects

 

 Now that we have the three models of our new building, we need to create the geometry objects to which the game will refer.  The geometry object exists so that the game engine will know how to show each model and its various levels of detail.  We will create a text file named “Geometries.con” to house the geometry object scripting.  Now that the file has been created, let’s open it and begin to create our objects.

    
        
GeometryTemplate.create StandardMesh Britain_Factory_M1

 

 The first line creates and names the geometry object.  “GeometryTemplate.create” specifies that we are creating an object.  “StandardMesh” is the type of object that is being created and “Britain_Factory_M1” is the name of the object that we are creating.

 

            GeometryTemplate.file Britain_Factory_M1

 

 The second line will specify the model (.sm file) that this geometry object will use.  We have specified that the model named “Britain_Factory_M1”.  BF:1942 assumes that the model will be in the “standardMesh.rfa” file unless you specify otherwise.

 

            GeometryTemplate.setLodDistance 0 0

 

 The “setLodDistance” line sets which LOD (Level Of Detail) of the model will be seen at a particular distance.  The first value, “0”, represents the first LOD that will be shown to a player (“1” would represent the second LOD, “2” would represent the third LOD, etc).  The second value, “0”, represents the distance at which you will begin to see the first LOD (Each subsequent LOD has less detail than the one before it.).

 

 The model we are using for our close-up model was only exported with three LODs, so it will only need three “GeometryTemplate.setLodDistance” lines.  When we have completed the scripting for this object it will look like this:

 

GeometryTemplate.create StandardMesh Britain_Factory_M1

GeometryTemplate.file Britain_Factory_M1

GeometryTemplate.setLodDistance 0 0

GeometryTemplate.setLodDistance 1 50

GeometryTemplate.setLodDistance 2 100

GeometryTemplate.setLodDistance 3 200

GeometryTemplate.setLodDistance 4 400

GeometryTemplate.setLodDistance 5 800

 

It is also best to setup a simple version of the building for long distances. This version should have significantly less poygons. 

GeometryTemplate.create StandardMesh Britain_Factory_L1

GeometryTemplate.file Britain_Factory_L1

GeometryTemplate.setLodDistance 0 0

GeometryTemplate.setLodDistance 1 50

GeometryTemplate.setLodDistance 2 100

GeometryTemplate.setLodDistance 3 200

GeometryTemplate.setLodDistance 4 400

GeometryTemplate.setLodDistance 5 800

 

 We will finish the Geometry objects by creating the last object for our wrecked model of the building.  We will call the object “Britain_Factory_wreck_m1” and it will use the “Britain_Factory_wreck_m1.sm” file located in the “StandardMesh” directory.

 

GeometryTemplate.create StandardMesh Britain_Factory_wreck_m1

GeometryTemplate.file Britain_Factory_Wreck_m1

GeometryTemplate.setLodDistance 0 0

GeometryTemplate.setLodDistance 1 150

GeometryTemplate.setLodDistance 2 200

GeometryTemplate.setLodDistance 3 250

GeometryTemplate.setLodDistance 4 300

GeometryTemplate.setLodDistance 5 500

 

 Now that we have finished creating the Geometry objects, save this file and we will begin the necessary scripting to create the destroyable building.

 

STEP 2: Scripting for the Destroyable Building

 

 A normal building in BF:1942 is not affected by weapons, vehicles, etc.  A building needs to have hitpoints to be destructible and the only way to accomplish this is to script the building so that it is a “Player Controllable Object” (PCO).  The PCO will contain all of the hitpoint settings as well as any special effects that you might want to use with this building (smoke stacks, flashing lights, etc.).

 

            ObjectTemplate.create PlayerControlObject Britain_Factory

 

 The first line will create a PCO named “Britain_Factory”. (Note that object scripting begins with “ObjectTemplate”, just as geometry scripting begins with “GeometryTemplate”.)

 

            ObjectTemplate.setNetworkableInfo FactoryBodyInfo

 

 The second line will define how the status of the building will be communicated in multiplayer games.  “ObjectTemplate.setNetworkableInfo” tells the game engine that the building status will be communicated using the “FactoryBodyInfo” network object.

 

ObjectTemplate.cullRadiusScale 5

ObjectTemplate.hasMobilePhysics 0

ObjectTemplate.explosionRadius 20

ObjectTemplate.explosionDamage 5

ObjectTemplate.drag 2

ObjectTemplate.dragOffset 0/0/0

ObjectTemplate.mass 4500

 

 The next few lines configure a few necessary things for a PCO, including mobile physics (“0” is off, “1” is on), the size of the explosion, how much damage the explosion will inflict, the mass of the building, etc.  Mobile physics are turned off for the building because it will be stationary.  The values set for the explosion radius and damage can be adjusted at any time (the higher the value, the larger the explosion) and the mass should always be more than “2500”. 

 

ObjectTemplate.hasCollisionPhysics 1

ObjectTemplate.hasResponsePhysics 1

 

These two lines turn on collision and response physics for the PCO.  The collision and response physics are needed (in tandem with the collision mesh of the model) so that a player can’t walk through the building and so a weapon can inflict damage on the building.

 

        ObjectTemplate.hasArmor 1

ObjectTemplate.speedMod 2

ObjectTemplate.hitpoints 20

ObjectTemplate.maxhitpoints 20

 

  The PCO’s hitpoints and armor is configured in the next four lines.  The higher the hitpoints, the more damage it will take to destroy the building.  The “has Armor” value must be set to “1” because if note, the PCO can’t be damaged.

 

ObjectTemplate.timeToLiveAfterDeath 10

ObjectTemplate.material 45

ObjectTemplate.criticalDamage 10

 

  The “timeToLiveAfterDeath” is the time the wrecked model will exist before fading away.  As mentioned in Step 1, the type of material defines how the PCO will take damage from different weapons.  The “criticalDamage” value defines how many hitpoints the PCO must have to be considered critically damaged.  You could add another line using “ObjectTemplate.hplostwhilecriticaldamage” to set how many hitpoints will be taken away from the PCO once it is critically damaged.

 

            ObjectTemplate.addTemplate lodfactory

 

            This line adds the LOD object that we will create later.

 

         ObjectTemplate.GUIIndex 30

ObjectTemplate.setSoldierExitLocation 6/12/-7 180/0/0

ObjectTemplate.setVehicleIconPos 40/60

ObjectTemplate.setNumberOfWeaponIcons 0

ObjectTemplate.setVehicleCategory VCLand

ObjectTemplate.setVehicleType  VTArtillery

ObjectTemplate.setToolTipType  TTArtillery

 
The setMiniMapIcon command lets you put an icon on the map that looks like a factory. This way you can tell if the object has been destroyed or not for objective based gameplay.
  

ObjectTemplate.setMinimapIcon "Minimap/minimap_icon_Factory_32x32.tga"

 

The final line we will need for the creation of the PCO sets the file location of the icon image that will be used to identify the position of the PCO on the minimap.

The next object we will need to create is the LOD object.

 

ObjectTemplate.create LodObject lodFactory

ObjectTemplate.hasMobilePhysics 0

ObjectTemplate.hasCollisionPhysics 1

ObjectTemplate.hasResponsePhysics 1

rem -------------------------------------

ObjectTemplate.addTemplate FactoryComplex

ObjectTemplate.addTemplate FactorySimple

ObjectTemplate.addTemplate FactoryWreck

rem -------------------------------------

ObjectTemplate.lodSelector FactoryLodSelector

           

BF:1942 uses LOD objects to control which model is shown to a player at any given time.  The LOD object is imbued with collision and response physics.  There are three templates added to this LOD object; FactoryComplex – close-up view of the building; FactorySimple – the low polygon view of the building from long distance; and FactoryWreck – the wrecked view of the building.  The final line defines the “lodSelector”.  The “lodSelector” object makes the decisions about which model of the building a player will see.

 

***NOTE: The order of the templates added into an LOD object is very important.  The first template must be the high polygon model, followed by the low polygon model and the wrecked model last.

 

FactoryComplex:

 

 

         ObjectTemplate.create Bundle FactoryComplex

ObjectTemplate.geometry Britain_factory_m1

ObjectTemplate.setHasCollisionPhysics 1

 

We will create the “FactoryComplex” object first.  It will be set up as a “Bundle” because we may want to include other objects within this object (ladders, special effects, etc.).  The “ObjectTemplate.geometry” line defines which geometry object (model) this object will use.  We created the geometry objects that we are referencing in Step 1.

 

FactorySimple:

 

 

ObjectTemplate.create SimpleObject FactorySimple

ObjectTemplate.geometry Britain_Factory_L1

ObjectTemplate.hasDynamicShadow 1

 

FactoryWreck:

 

 

ObjectTemplate.create SimpleObject FactoryWreck

ObjectTemplate.geometry Britain_Factory_Wreck_m1

 

The other two objects will be a “SimpleObject” because it is only a model that will be shown, with no dynamic properties.  If a model is exported with a shadow, you can enable this via the “ObjectTemplate.hasDynamicShadow” line.

 

LodSelectorTemplate.create DistCompareSelector2 FactoryLodSelector

LodSelectorTemplate.hasDestroyedLod 1

LodSelectorTemplate.addLodDistance 300

 

The final object we will need to create in this file will be the “lodSelector” object that we referenced within the LOD object earlier.  If you plan to have a wrecked model shown when the building is destroyed, you must include the “LodSelectorTemplate.hasDestroyedLod 1” line. 

 

            We have finished all the scripting we need to complete for the ‘Objects.con” file.  Save and close the file.

 

 

STEP 3: Creating the Network Objects

 

For a destroyable building to work in a multiplayer game there needs to be some way for the game to communicate the status of the building to other clients.  BF:1942 does this using network objects.  A networkable object is usually reference in an object using the “ObjectTemplate.setNetworkableInfo” line.  Back in Step 2, we created a line stating “ObjectTemplate.setNetworkableInfo FactoryBodyInfo”.  Now we need to create the object we referenced.

Network objects are usually kept in a file titled “Network.con”.  Create a file named “Network.con” and open it.

 

            NetworkableInfo.createNewInfo FactoryBodyInfo

 

             “NetworkableInfo.createNewInfo” indicates that we are creating a new network object.  The value “DesBuildingBodyInfo” will be the name of the new object.

 

            NetworkableInfo.setPredictionMode PMLinear

 

  The second and final line of the network object sets the way the data will be exchanged over the network.  “PMLinear” is the standard value that is used for all networkable data.

 

Those two lines are all that is needed to create the necessary network object.  The network object is the final piece of scripting that is needed to create the destroyable building.  The “Network.con” file can be saved and closed.

 

Final Notes

 

The tutorial has shown you how to create a basic destroyable building.  A mini-mod has been created as an example and employs the scripting listed above.  You can download it here. Once downloaded, unzip into the Battlefield1942\Mods folder.