Left 4 Dead 2

Left 4 Dead 2

Not enough ratings
Make a weapon replace another ( not script ) ( recompile )
By Artillery Rain
I also make video about this : https://steamhost.cn/steamcommunity_com/sharedfiles/filedetails/?id=2977656020

So let's say you have an awesome weapon mod, it's just the best except... you want it to replace another weapon. The progress is pretty simple, but odd enough i haven't seen any guide for this matter, so here i am.
This guide applies to almost any replacement swap, be it M16 --> SCAR , Military Sniper --> Hunting Rifle , UZI --> MP5 , or more wild conversion like AK47 --> Military Sniper , Chrome Shotgun --> AWP , Pistols --> UZI , and so on.
Be mindful when publishing stuffs, and respect original creator.
* IMPORTANT : Though i'm here writing this guide, i myself still have much more to learn and understand how this game work. My method is different with guides from famous and proper modder of Source games, and shouldn't be used as a reference to learn modding .
* IMPORTANT : .mdl after decompilation won't work the same with when it was, considerable lose are anim_layer and look_poses sequence since they usually use "delta" option to make them blend with "idle" sequence.
Therefore those "anim_layer" are useless and you must use "anim" and ehhh whatever if you encounter such thing then comment for further instruction.
   
Award
Favorite
Favorited
Unfavorite
Summarize
- Get the mod you want.
- Extract it. Navigate to it's v_model and decompile *.mdl .
- Edit *.qc
... --- $modelname
... --- $definebone and $bonemerge
... --- $attachment ( if you work on dual pistol )
... --- $sequence order
... ... --- edit animation and fps
... --- sound redirect
- Compile
- ( do the same for w_model )
- Edit sound
- Pack into *.vpk
Preparation
Some foreword
NOTE : In some case, decompilation may fail due to *.mdl version mismatch. Make sure to check Crowbar's log and all expected file. If it fail to decompile, try your luck with "Override MDL version" setting, one of them will do.
When all set, backup all of them, just in case your cat jump onto keyboard and mess thing up.

Now gaze straight to *.qc file. It's a text that contain parameters, values, paths, ... to compile *.mdl . We will go through all important line ( well pretty much the whole file ) so grab yourself a drink .
FIRST OF ALL
Look for $illumposition (it's to define lighting position), delete it. When compiling it will automatically generate a correct value (should be the center of the model geometry), and in case you use another animation, old value will no longer valid so it may look weird.

Scroll down until you see a block of $definebone line, next to it should be another block of $bonemerge line. Cut all of them and paste it at this location.

$modelname
This parameter define which weapon the model will replace in game.
YES THE THING WE NEED IS RIGHT AT THE START!
Well, you are right, editing it alone can swap weapon replacement. If that's all you need, then mission completed.
I know it would be nice if i list all model name here, but i'm too lazy for it. Sorry for inconvenience.
Now game will recognize your weapon as the one you assigned. But there is more step to make your gun function correctly.
$attachment
You can just leave it as it is. But be mindful of them when you work with Dual Pistol. it uses different attachment (e.g., instead of "muzzle_flash" it uses "muzzle_flash_r" and "muzzle_flash_l" for right and left respectively).
If you make a SMG model replaces Dual Pistol, you can just attach both muzzle_flash with same bone and offset value.
If you want to reposition these attachment, hlmv.exe will come in handy. In Crowbar, click View tab, throw a .mdl into it, then click "View as Replacement".
$sequence
This is the biggest piece of this compile sheet. The order of these $sequence must match with default L4D2 weapon you are gonna replace. If they don't, the mod won't work well in online game. So let's sort them all in proper order.

Different weapon have different timing ("deploy" and "reload"), and they depends on number of frame that anim has and fps it plays. The easiest way to adapt old anim to match with new weapon replacement is adjust fps. Higher fps make anim plays faster.

Important Note : Each sequence has both *animation* and *animation_layer* . I believe all guide about weapon modding went through this matter, but i will recap.
*animation* decide the amount of frame *animation_layer* will play. So YES, it could be anything, as long as it has equal or more frame than *animation_layer* . If it has less frame than it's partner, *animation_layer* will stop at *animation* last frame, and the remain will never be used.
*animation_layer* is what you will see in game. That's why all other parameter like fadein fadeout, ik rule, particle and sound are set in this sequence.

But it only applies to guns. Melee weapons seems to have different rule regarding anim and anim_layer. (it's Valve's fault so you can't argue about it)
---------------------------------
Now one more step, see those { event 5004 ... _______} line? It tells game to play what sound at what frame when an anim is playing. You gotta edit it to use sounds from the replacement you chose.
w_model
You have your v_model good to go, now you gotta convert w_model to your replacement too. Don't worry it's much more easier.
Simply edit $modelname to the one of your desire. Don't forget to delete $illumposition too.
sound
Earlier you made your gun use sound from another replacement, so now you gotta make sound for it. Navigate to "sound" folder, then "weapons", now you will rename the one in front of you into your replacement's name. Then move in until you see those .wav file, check and rename them to your replacement.
* Note : If those sound are shorter than default sound, game will automatically "generate" noises. This is sound cache error and you can't fix it even when you use snd_rebuildaudiocache.
Regardless, always run snd_rebuildaudiocache or snd_updateaudiocache when you have audio problem. snd_updateaudiocache will scan for modified sounds first then create cache for them, instead of creating cache for every sound. You can run snd_updateaudiocache even when playing, and if so, type snd_restart.
Packing and testing
There is addoninfo.txt that display mod's info when you browse it in Add-on menu in game, make sure you edit it too to avoid confusion. Now everything is ready, pack your .vpk , bring it to "addon" folder, open game and test it.
Depend on error you encounter, edit your v_model or sound.
v_model :
- If deploy and reload timing aren't quite right, change fps value.
- If it doesn't use the right sound, or missing sound, check { event 5004 ... ___ } .
sound :
- If sound is bugged and you can't fix it with snd_rebuildaudiocache , consider replacing them with another sound mod or edit those sound yourself.
- If gun still uses default sound, check your sound to see if they have correct name.
------------------------------------------------------
Feel free to comment if you need further explanation.
Result : https://www.youtube.com/watch?v=nNC6uvjnq7I
11 Comments
Artillery Rain  [author] 7 hours ago 
Use the view model feature in Crowbar to check if your fire.smd, reload.smd, melee.smd, etc... works. If they don't then it's no use.
Artillery Rain  [author] 7 hours ago 
Edit your .qc (find and replace) as follow:
fire_layer.smd --> fire.smd
reload_layer.smd --> reload.smd
melee_layer.smd --> melee.smd

then either:
* In each of corresponding $sequence, find and delete these line:
delta
subtract "@..." 0 (high chance they starts with @)
Artillery Rain  [author] 7 hours ago 
* Edit these line:
subtract "@..." 0 --> subtract "a_idle_1" 0 (with a_idle_1 being the idle animation, specified by a $animation above within your .qc)
ikrule "lhand" touch "(your gun's parent bone)" (check idle $animation for bone's name)
ikrule "rhand" touch "(your gun's parent bone)"
Those 2 lines glue your hands to your weapon, so the edit must be made accordingly, for example:
- When firing, if both hands hold your gun the whole time, then edit as above
- When reloading, if left hand moves to change mag, right hand remain on grip the whole time, then edit like this:
ikrule "lhand" release
ikrule "rhand" touch "(your gun's parent bone)"
everlasting echo 11 hours ago 
after porting a p90 mod made for uzi to the mp5, most things resulted in a pretty satisfactory result EXCEPT for the fact that the hands are clipping way behind where it's supposed to in the firing, reloading, and melee animations, i wonder if there's any way to repair this?
Artillery Rain  [author] 19 Jun @ 12:27am 
What error did you see in log?
Crabby Cakes 18 Jun @ 8:18pm 
the recompiling process is extremely unclear.
R0TTEN_C0RPSE 23 Apr @ 6:29am 
For some reason i cannot recompile the qc file
Artillery Rain  [author] 26 Oct, 2024 @ 7:31pm 
You can try to remove look_poses.
Also does it go off when running?
JollyBeany 26 Oct, 2024 @ 3:56pm 
I managed to make a weapon that was originally a pistol become the magnum but when I have it equipped, it goes off to the side. This only happens when I'm idle
Artillery Rain  [author] 16 Aug, 2024 @ 3:30am 
Aside of writing it yourself, i suggest you copy $sequence from others as it has "activity" (varies from weapons especially melee) and "event" (particle and sound).
You may also need to delete "ikrule" in $sequence unless you know how to deal with them.