Left 4 Dead 2

Left 4 Dead 2

36 ratings
Creating Weapons Scripts
By Nate
Process and Theory behind editing weapon_xxx.txt scripts for the game.
   
Award
Favorite
Favorited
Unfavorite
Prerequesities
Crowbar
You will need to use the program Crowbar in order to unpack the game's files. I won't go over downloading and configuring the program, you should have the wherewithal to accomplish that yourself.

SDK
You will also need to install the L4D2 SDK. Find this in the Steam Library/Tools section. Once the SDK installs, you will find the programs needed in the Left 4 Dead 2/bin folder. You can use the workshop manager if you'd like, but I use Crowbar for publishing content. In any event, in order to pack the folders into workshop format .vpk, you will need to use vpk.exe. You can probably find this .exe in any of the source game SDKs, but if you don't have any just install the one for L4D2.

Notepad/Notepad++
You'll be editing simple text files, so why not do it in style?
Extracting Vanilla Scripts
Unpacking the Game's Files
Open Crowbar or if you've set it up to open on double-click, open the file /Left 4 Dead 2/left4dead2/pak01_dir.vpk, and wait for the file to load.

On the folder tree pane, navigate to the "Scripts" folder.

In the main window, find the entries starting with "weapon_"xxx.txt. Highlight the scripts you want to modify and drag them out to a new folder on the Desktop or your workspace.

Lather, rinse and repeat for the files /Left 4 Dead 2/left4dead2_dlc1/pak01_dir.vpk, /Left 4 Dead 2/left4dead2_dlc2/pak01_dir.vpk, and /Left 4 Dead 2/left4dead2_dlc3/pak01_dir.vpk... extract to the folder and overwrite if prompted to ensure you are using the latest version of each weapon's script.
Setup Your Workshop Item's Folder
Structuring Your .VPK
Once you decide which weapon's stats you will edit, make a folder for it.

The folder structure should be as follows:
[ ] name of your mod (ex: scriptFor357Magnum) [ ] scripts [ ] weapon_xxx.txt (the weapon's script) [ ] addoninfo.txt [ ] addonImage.vtf (optional) [ ] addonImage.jpg (also optional, and mine never seem to work)
addoninfo.txt
Describe Your Script
Edit the addoninfo.txt file in order to give your mod descriptors while in game. This helps let people know what your mod does and to which weapon it applies.

Sample addoninfo.txt
You can copy/paste this directly, if you'd like. Just change the relevant values, please.
"AddonInfo" { AddonTitle ".50 GS/Desert Eagle (Desert Eagle)" AddonVersion "1.0" AddonDescription "Weapon Script for PYgame.D.Cthulhu's .50 GS/Desert Eagle From COD:MW 2019: https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=2249842917." AddonAuthor "YOUR NAME HERE" AddonAuthorSteamID "YOUR ID HERE" AddonURL0 "" AddonSteamAppID 550 AddonContent_BackgroundMovie 0 AddonContent_BossInfected 0 AddonContent_Campaign 0 AddonContent_CommonInfected 0 AddonContent_Map 0 AddonContent_Music 0 AddonContent_Prefab 0 AddonContent_Prop 0 AddonContent_Script 1 AddonContent_Skin 0 AddonContent_Sound 0 AddonContent_Spray 0 AddonContent_Survivor 0 AddonContent_Weapon 0 AddonContent_WeaponModel 0 }

I won't comment on the structure of the file, these tags are pretty self explanatory.
Research Your Weapon (Optional)
To Realism, or not to Realism?
My whole goal of creating/editing weapon scripts is to make them more realistic as the game allows. To do this, I research the weapons and snag their real world values.

I use weapon-systems.net, military-today.com, modernfirearms.net, manufacturer's specs, and Wikipedia to get the physical attributes and characteristics for the weapons.
Convert Your Values to In-Game Equivalents (Optional)
Converting for Realistic Values
I have a Google Spreadsheet in which I have embedded a number of conversion formulas for public use.

Find it here: https://docs.google.com/spreadsheets/d/1hn2ELPDXtpCPwgMsQv3zrGIPY_5OIn9e2kmqb7CEfgI/edit#gid=1065007544.

I use the following values from real-world weapons:
    weight in grams/ounces/pounds
    real-world rate of fire
    weapon's effective range in meters/yards/feet
    weapon's maximum range in meters/yards/feet
    ammo's real-world penetration depth in inches/millimeters

Prepare Your Edits
Choosing Which Values to Change
Almost time to change the values in your weapon script.

Just because you can, doesn't mean you should. There are many variables that can be changed but probably should not be. I do not know what every variable does. I believe many are carryovers from Counter-Strike or some other Source game, which explains variables like "Price" and "WeaponArmorRatio".

I used this old guide: https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=1060964738 along with some trial and error and in-game testing to figure out what values control what.

The values I edit are generated using formulas and a little hocus-pocus. Basically: the weight is based off of the weapon's real life weight, converted to in-game units; the vertical recoil is influenced by the power of the weapon as well as the weight of the weapon; the maximum spread/recoil is influenced by the power and the rate of fire; the spread decay, or recovery is influenced by the rate of fire and the weight of the weapon; the penetration power is based off of the ammunition's real-world penetration depth of 10% ballistic gelatin molds, converted into game units; and finally, the character penetration distance influenced by the power and range of the weapon.

I will clarify what I understand each of these values to mean/do below:
Annotated Sample weapon_.txt Script
WeaponData { // Terror-specific Data -------------------- "VerticalPunch" "1.50" //*** Control the upward recoil "SpreadPerShot" "0.25" //*** How much much deviation the bullet will have on the next shot "MaxSpread" "35.63" //*** The maximum amount of spread/deviation "SpreadDecay" "6.67" //*** How much spread is mitigated/recovery speed "MinDuckingSpread" "0.34" //*** Factor(?) How much spread (/shot) whilst crouching "MinStandingSpread" "0.90" //*** Same as above, while standing still "MinInAirSpread" "1.24" //*** Same as above whilst jumping "MaxMovementSpread" "2.48" //*** Same as above whilst moving "AddonAttachment" "primary" //*** Can apply addons like laser? "Team" "Survivor" "Tier" "2" //*** 0, 1, or 2. The "class" of the weapon. "ResponseRulesName" "Rifle_Desert" //*** Believe this is what the survivor calls out when they see the weapon // particle muzzle flash effect to play when fired "MuzzleFlashEffect_1stPerson" "weapon_muzzle_flash_assaultrifle_FP" "MuzzleFlashEffect_3rdPerson" "weapon_muzzle_flash_assaultrifle" // model for the shell casing to eject when we fire bullets "EjectBrassEffect" "weapon_shell_casing_rifle" // Used in the music system when this weapon fires "MusicDynamicSpeed" "0.35" "DisplayName" "#L4D_Weapon_DesertRifle" "DisplayNameAllCaps" "#L4D_Weapon_DesertRifle_CAPS" "NewInL4D2" "1" // 360 Terror Data "MaxAutoAimDeflection1" "10.0" "MaxAutoAimRange1" "0" //This value determines how "big" a target is for auto aim. If a target is 10.0 units big then it is considered 10.0*scale. //You can think about this value controlling a falloff value on distant targets, the smaller the value the harder it is to hit at a distance. "WeaponAutoAimScale" "1.0" // End Terror-specific Data ---------------- "Rumble" "4" //*** vibration type/strength "MaxPlayerSpeed" "230" //*** How fast you walk/run holding this weapon "WeaponType" "rifle" "WeaponPrice" "3100" "WeaponArmorRatio" "1.4" "CrosshairMinDistance" "4" "CrosshairDeltaDistance" "3" "BuiltRightHanded" "1" "PlayerAnimationExtension" "m4" "CanEquipWithShield" "0" // Weapon characteristics: "PenetrationNumLayers" "2" //*** How many "things" typically bodies a bullet will go through "PenetrationPower" "40" //*** I believe this value is: the bullet will travel through objects this thick "PenetrationMaxDistance" "0" //*** The maximum amount of distance the bullet will travel and still penetrate. I believe 0 means no max distance, therefore will engage in penetration until number of layers is reached (gigitty) "CharacterPenetrationMaxDistance" "3632" //*** The game will ignore the penetration details above until the bullet has traveled this far, so basically unlimited penetration within this distance (?) "Damage" "88" //*** HP damage dealt per shot, or per pellet in the case of shotguns "Range" "42084" //*** The bullet "lives" for this distance in hammer units (?) after this distance the bullet no longer travels/exists "GainRange" "28871" //*** After this range, the bullet damage scales down to zero (quickly!) "RangeModifier" "0.930342" //*** How quickly the damage is reduced per unit of distance... "Bullets" "1" //*** Number of projectiles per shot. Increased for shotgun shells "CycleTime" "0.0632" //*** How rapidly the weapon cycles/weapon's rate of fire. Find the conversion calculator on the spreadsheet "TimeToIdle" "1.5" //*** How long before weapon enters idle animation "IdleInterval" "60" // Weapon data is loaded by both the Game and Client DLLs. "printname" "Assault Rifle" "playermodel" "models/w_models/weapons/w_desert_rifle.mdl" "viewmodel" "models/v_models/v_desert_rifle.mdl" "CharacterViewmodelAddon" { "Coach" "models/weapons/arms/v_arms_coach_new.mdl" "Mechanic" "models/weapons/arms/v_arms_mechanic_new.mdl" "Producer" "models/weapons/arms/v_arms_producer_new.mdl" "Gambler" "models/weapons/arms/v_arms_gambler_new.mdl" "Manager" "models/weapons/arms/v_arms_louis.mdl" "Biker" "models/weapons/arms/v_arms_francis.mdl" "TeenGirl" "models/weapons/arms/v_arms_zoey.mdl" "NamVet" "models/weapons/arms/v_arms_bill.mdl" } "anim_prefix" "anim" "bucket" "0" //*** Used for 360/game controller inventory "bucket_position" "0" //*** Same as above "clip_size" "30" //was 60 //*** How many rounds per clip/magazine "primary_ammo" "AMMO_TYPE_ASSAULTRIFLE" //*** Controls total ammo pool size, as well as additional effects. SNIPER ammo has infinite penetration, MAGNUM and MACHINE GUN ammo instantly kill any common infected at any distance on any difficulty "secondary_ammo" "None" "weight" "20" //*** Not the actual weight in MASS, but rather, the priority of the weapon. Bots will pick up weapons with higher priority (25 > 5). "item_flags" "0" //*** Not used, used in other source games, controls things like can you lose all ammo or does it automatically reload "LoadoutSlots" "2" //*** What inventory item this takes up, primary, secondary, or throwable, etc // Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds) SoundData { "single_shot" "Rifle_Desert.Fire" "shoot_incendiary" "Rifle_Desert.FireIncendiary" } // Weapon Sprite data is loaded by the Client DLL. TextureData { "weapon" { "file" "vgui/hud/icon_rifle_desert" //*** Scripts that alter this screw up icon replacements!! "x" "0" "width" "256" "height" "64" } "ammo" { "file" "vgui/hud/iconsheet" "x" "384" "y" "448" "width" "64" "height" "64" } "crosshair" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } "autoaim" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } } ModelBounds { Viewmodel { Mins "-10 -2 -13" Maxs "30 10 0" } World { Mins "-8 -9 -6" Maxs "29 9 8" } } }
Make Your Edits
Start Editing Away
Open the weaponxxx_.txt file in Notepad/++. Using the annotated sample or previously referenced guide as a, well, guide, make your changes to the values of the variables.

There are some suggested ranges and notes again in the previously referenced Google Spreadsheet. I summarized them from the aforementioned guide. However I tended to disregard the suggested values.

Save Your Changes
CTRL+S. You didn't really need me to tell you that, did you?
Pack Your ♥♥♥♥
Time to Pack Up the VPK
Now is the easy part. Find your folder for your addon. Then browse to /Left 4 Dead 2/bin. Drag your addon folder onto the file vpk.exe. It should pack up your folder and create a .vpk file.
Test Your Script (Optional?)
Test!! (Or Wait for the Complaints and Criticisms to Come Rolling in!)
To test your new script locally is very easy. Firstly, make sure you are not subscribed to outside weapon script mods for the weapon you are testing. So if you are testing your edits to the Magnum Pistol, make sure you are unsubscribed from any Magnum Pistol scripts in the workshop.

Drag your newly created .vpk file into your addons folder. This folder should be /Left 4 Dead 2/left4dead2/addons.

Now start the game and go into the Addons... menu. Your newly made .vpk, with its proper descriptive info, should be among the first displayed at the top of the list. If it isn't, you've done something wrong.

If it is, go ahead and test it in game. I like to use TUMTARA, since it is quick and easy to load up and has all the weapons right there to access. But you can test however you like. Just make sure to notice if the changes you made have stuck, for example, magazine size, rate of fire, recoil, etc.

If you don't notice the changes, open the Console (~) and check for errors (red text). If you don't have errors, go back to your Addons menu, and make sure there are no conflicts or red text there.

If you don't have any errors and conflicts and your changes still haven't taken, start over, you may have missed a step (are you sure you saved changes?? CTRL+S!!)...
Time to Share!
Publish Your Addon

You have two options, you can use the built-in workshop manager or Crowbar. The vanilla manager is in the /Left 4 Dead 2/bin folder. I used this but it does take a long time to load, and the text boxes can be a bit wonky. I won't cover how to publish using this because it is pretty easy to figure out.

Using Crowbar is much preferred, as you can easily see all your addons at once, and can save drafts and templates for easier republishing.

Assuming you've already set up the Crowbar program, go to the Publish tab.

Choose Left 4 Dead 2 as the Game Option.

Give your Addon a Title.

Write a Description that helps people find it in the workshop.

Check the appropriate Tags checkboxes on the right-hand side of the screen. This will help it come up in workshop search results.

Click the Browse... button for the Content File field. Navigate to your newly made .vpk (still in the /Left 4 Dead 2/left4dead2/addons folder? Time to move it!) and select it.

Add a Preview Image in the field below content. This is a require image for the workshop and there are specific size/format parameters. I believe it has to be a 640x360px .jpg image... mine never seem to show up in game though so I must have something wrong?

You can change the Visibility of your mod using the drop down menu. Obviously Public is required if you want people to be able to search for and see it.

The Save as Template button is invaluable if you will be making multiple scripts/addons. Once you have a Template you've begun editing, this changes to Save as Draft.

The last step is to click on Publish, and your content will go live!

Find it in your Steam workshop and Subscribe to activate it. Double check it shows up in the Addons... menu in your game and you're golden.

????

Profit.

Enjoy! =)

12 Comments
Nate  [author] 15 Jul @ 9:20pm 
@7274
Yes, these values are hardcoded in and affect weapon properties. Sniper ammo has infinite penetration. Magnum ammo gibs and kills commons in 1 shot. Same with M60. I may experiment with vscript in the future. Just now getting my wings back.
7274 14 Jul @ 8:55am 
I'm late for this but changing the ammo type is the only way you can change the max ammo value for the weapons, just be aware the wound type also changes along with it
_-Echo-117-_ 12 Feb @ 11:41pm 
hello! you know how can i change the max ammo value for a weapon?
Nate  [author] 17 Jan @ 11:28pm 
@idiot 3
This is not the way.
L0W_1Q 29 Aug, 2024 @ 10:39pm 
Thanks, mate!
Nate  [author] 29 Aug, 2024 @ 3:01pm 
@L0W_1Q
Your question is ambiguous but I'll try to answer anyways.

If you download a weapon model that includes its own HUD icon (or even some that do not) there's a 50/50 chance it is compatible with an external weapon scripts mods.

If the weapon model creator has included a weapon script, you will have to extract the contents of .vpk file with Crowbar, delete the "scripts" folder. Then repack it using vpk.exe.

Ideally custom HUD icons are not included with the weapon script, but separately. Include HUD icons with weapon models simply by putting it in the proper location, something like materials\vgui\hud\icon_pistol or whichever.

I hope that answers your question.
L0W_1Q 29 Aug, 2024 @ 10:26am 
How to make HUD icons compatibility? If the model already has custom HUD icon, my scripts doesn't work.
Nate  [author] 21 Apr, 2024 @ 10:41pm 
@VorteX
I'm not sure I understand your question. If you mean can you tactical reload weapons as in, drop and replace the mag and not have to re-rack the weapon by cocking it or pulling the charging handle, then I'm not sure that's a behavior actually controlled by these types of scripts.
VorteX 21 Apr, 2024 @ 1:04am 
is tactical reload possible on all guns like the deagle?
小布 21 Feb, 2024 @ 5:31am 
good