[Tutorial] Absolute beginners guide to weapon creation. -1 reply

Please wait...

metal_militia

Killing is my business...

50 XP

29th November 2004

0 Uploads

1,759 Posts

0 Threads

#1 13 years ago

[color=Black]When i first started weapon modding i really wanted to see something like this just so i could get a intro to the tools and techniques... but alas i couldn't find one. So now i have a fairly good idea of what im doing I thought i'd write one myself. Let us begin with the basics. [/color]

[color=Red]YOU NEED VISUAL STUDIO. NOTEPAD DOES NOT WORK![/color]

[color=Black] Creating a base mod in steam. Nice and easy via the SDK. Just start the create a mod wizard from the SDK and follow the steps until you have a src folder on your hard drive. Steam_setup_1.gif Visual studio .NET 2003 setup When starting VS (visual studio) you will be confronted with a screen not dis-similar to this one... VS_setup_1.gif The only difference will be that you will not have any existing projects. Now we need to open the HL2 'solution'. Go to file > open solution VS_setup_2.gif And open the Game_sdk solution. VS_setup_3.gif Right. Now you should have a screen like this... VS_setup_4.gif Yours will not have any additional files open other than the start page. The important bit you should see is in the left hand box where it says 'client' and 'hl'. These are where the files you will be editing are stored. Rember to change the bit at the top which says 'Debug HL2' to 'Release HL2' or you will have to run the game in debug mode. Thats Visual studio set up for some hardcore, rockin' modding... erm yea. Anyway now for the hard bit. [/color][color=Black][color=Blue][color=Black][color=SeaGreen][color=Black]--------------------------------------------------------------------------------------------[/color][/color][/color][/color][/color] [color=Black][color=Blue][color=Black][color=SeaGreen][color=Black]Creating the gun. For this i will just copy and paste this article -> http://collective.valve-erc.com/index.php?doc=1102638409-05843300 But il try and add additional explanation to it and screenshots. My comments will be in red at the bottom of each paragraph. [/color][/color][/color][/color][/color]Introduction The purpose of this article is to help you add the code for new weapons to your Half-Life 2 mod. However difficult it may seem, I can assure you that the process is simple and straightforward once you understand how the existing weapons work. Note that this article only covers the coding aspect; new sounds, models, etc. for your new weapon are beyond the scope of what I will cover. Single-Player Weapons Background Weapons fall under a special class of entities known as network entities. This means that they exist as two objects, one instance on the server and one instance on the client. As such, both the server code and the client code need a class declaration for each weapon. The easiest way to see how the system is set up is to take an example and follow it through the SDK, so for the rest of the article we will use the Pistol as our example weapon. Server Let's start by taking a look at the Pistol weapon code on the server. It is located in weapon_pistol.cpp, under the "HL2 DLL" project folder. The first thing you'll notice is that both the class declaration and the implementation are in the same file. This is because there isn't any other object in the game that relies on the Pistol weapon declaration, and thus it can be put directly above the implementation. The second thing you'll notice is that CWeaponPistol is derived from a class named CBaseHLCombatWeapon. [color=Black]This is a base class of all the single-player Half-Life 2 weapons on the server. [/color]While the details of this inheritance structure are beyond the scope of this article, every new single-player weapon you create should derive from CBaseHLCombatWeapon at some point in its inheritance hierarchy. It should then override the methods it needs. Examining the current weapon code will give you a good idea of what is usually overridden, and for what purpose. [color=Red]Nah, i didn't really get this bit either. infact i dont really care where the names are derived from.. at the moment anyway. [/color]Client For most of the single-player weapons, all the prediction and networking code you need is handled by the base class implementation. In other words, it's already written for you. Only the weapons that require special client-side effects (such as the stun-gun and physics cannon) require additional coding. Take a look at the aforementioned examples to see exactly what a weapon would do to create special client-side effects. For the weapons you will usually be creating, there is a special file called c_weapon__stubs_hl2.cpp. A macro named STUB_WEAPON_CLASS creates a basic client-side class declaration for your weapon that does nothing but serve as the instantiable object on the client. [color=Red]This is a bit intersting.. well not really but its probably quite useful once you are an expert.[/color] Adding a Single-Player Weapon Now that you know a little about the structure of single-player weapons, how about adding one of your own? For this article we'll just copy the Pistol code verbatim, and give it a different name. Step 1: Create a new CPP file for your weapon. Name is weapon_.cpp, where is the name of your weapon. I used "killer" in place of , although you can use whatever in want in place of "killer." You can place the file anywhere, although the best place is probably with the rest of the weapon code (src\dlls\hl2_dll). [color=Red]To do this follow this procedure. File > Add new item GunCreation_1.gifGunCreation_2.gif Obviously replaceing with the name of your gun. [/color]Step 2: Open the Pistol code file (weapon_pistol.cpp) and copy all the code to your new file. [color=Red]This is found on the left hand menu. GunCreation_3.gif Make sure its set to solution explorer and not class view though (tabs at the bottom) [/color] Step 3: In your new file, change all instances of weapon_pistol to weapon_killer. Change all instances of CWeaponPistol to CWeaponKiller. There will be a bunch of #define's that use the word PISTOL. Change them to use KILLER instead - don't forget to change where they're referenced in the file as well. Lastly, change the console variable declared at the top of the file (of type ConVar) to use "killer" as well, and change all references to this variable. [color=Red]Do this with find and replace but be careful. Not all of the words 'pistol' need to be replaced. ONLY THE ONES IT TELLS YOU TO. I recommend using find and replace with the following options selected. GunCreation_4.gif [/color] [color=Red]And rember, its CASE SENSITIVE. [/color]Step 4: Your work on the server side is done. Now open up your client project, and go to the file c_weapon__stubs_hl2.cpp. Add your weapon to the list of stubs, following a similar format as the rest of them. [color=Red]not much to add here, just make sure you add it under '#ifndef HL2MP' and not the top ones. Also, case sensitivity still applies. [/color] Step 5: That's it! All the necessary changes to the code for your new weapon have been made. The final step involves creating a script file for your new weapon that contains resource information. Open up the folder \scripts and locate the file weapon_pistol.txt. Copy the file and name the new copy weapon_killer.txt. Open the file for editing. From here you can edit certain properties of your weapon. The most important change for the weapon to function properly in-game is for you to set up the bucket and position correctly. I left the bucket the same, and changed the position to 2 (since the Pistol and 357 Magnum occupy positions 0 and 1, respectively). [color=Red]The bucket is the place on the hud when you select the gun. For example bucket 0 bucket position 0 would be the first item you would select if you pressed the '1' key. [/color] And you're done. Load up your mod in Half-Life 2 and open a test map. Type give weapon_killer in the console. You should receive your new weapon! Typing impulse 101 at this point won't work because we haven't set up the gun to be given during that command, however you can do an impulse 101 followed by the give weapon_killer command to play around with the weapon for a while. Closing After the weapon has been added to your code, all that remains are the appropriate modifications to weapon models, sounds, and other such resources. [color=Black][color=Blue][color=Black][color=SeaGreen][color=Black]-------------------------------------------------------------------------------------------- Compileing your code Rather an important step. But very easy to do. In VS simply go to Build > Build solution. Now is when you fill find if you have made any errors and what they are. Note: you only need to do a full compile once. Once it is done, future compiles will only build the files you have modified and files affected by the ones you have modified. Once its compiled with no errors, your game should appear on the 'third party games' list in steam games, Now that wasn't so hard was it? ok now we can add custom models and sounds. i have been typeing for ages so im gonna take a break. il add the next part shortly or you can refer to my topic ' adding CSS weapons to HL2' ------------------------------------------------------------------------------------------------------ Custom sounds and models Ok tutorial part deux. [/color][/color][/color][/color][/color]Sounds Custom sounds are very easy to add.

  • First you need to rip a few sounds from somewhere. Lots to be had on the Interweb.
  • Create a folder called 'sound' in you root mod directory and then put a folder in it called 'weapons'.
  • Now, put the sounds into the weapons folder.
  • Next you need to find the script file named 'game_sounds_weapons.txt' open it up and you should see a number of entries like these...

[color=Green]// weapon_ar2.txt "Weapon_AR2.Empty" { "channel" "CHAN_WEAPON" "volume" "0.7" "soundlevel" "SNDLVL_NORM" "wave" "weapons/ar2/ar2_empty.wav" } "Weapon_AR2.Reload" { "channel" "CHAN_ITEM" "volume" "0.7" "soundlevel" "SNDLVL_NORM" "wave" "weapons/ar2/ar2_reload.wav" }[/color] You need to add some more sound types.. .for example //weapon_mp5.txt [color=Red]-Comment. this just tells you where the script your using the sound in is.[/color] "weapon_mp5.single" [color=Red]-[/color][color=Green][color=Red]Name you will use to 'request' the sound in the weapon's script[/color].[/color] { [color=Red]-Start of code[/color] "channel" "CHAN_WEAPON" [color=Red]-Chanel to play sound through[/color] "volume" "0.7"[color=Green] [color=Red]-[/color][color=Red]Volume of the sound ingame.[/color][/color] "soundlevel" "SNDLVL_NORM"[color=Green] [color=Red]-not sure about this[/color][/color] "wave" "weapons/mp5/mp5-1.wav" [color=Red]-The location of your sound file[/color] } [color=Red]-end code[/color] This is all quite self explanitry when you are actually doing it.

  • Now thats done, change the weapon's script file to use the sounds.

For example i would put in my MP5's script file..... "reload" "Weapon_SMG1.Reload" "reload_npc" "Weapon_SMG1.NPC_Reload" "empty" "Weapon_SMG1.Empty" "single_shot" "weapon_mp5.single" [color=Red]-most important one to change[/color] "single_shot_npc" "weapon_mp5.single" "special1" "Weapon_SMG1.Special1" "special2" "Weapon_SMG1.Special2" "double_shot" "weapon_mp5.single" "burst" "Weapon_SMG1.single" I left the special and empty alone. The default sound will do for now. Thats that done... go test it. [color=Black][color=Blue][color=Black][color=SeaGreen][color=Black] Models [/color][/color][/color][/color][/color][color=Black]This is very easy. Simply open the script for the weapon you created. (root/scripts) and you should see something like this... [color=Green]// Small Machine Gun 1 WeaponData { // Weapon data is loaded by both the Game and Client DLLs. "printname" "#HL2_SMG1" "viewmodel" "models/weapons/v_smg1.mdl" [color=Red]-Change this to the path of your model.[/color] "playermodel" "models/weapons/w_smg1.mdl" [/color][/color][color=Black][color=Green][color=Red]-Change this to the path of your model.[/color][/color][/color] [color=Black][color=Green] "anim_prefix" "smg2" [color=Red]-Change this to the weapon which best fits yours model.[/color] "bucket" "3" "bucket_position" "2"[/color] And thats it, provided you have all the models and have the path set up right your weapon should have a nice new model.[/color] [color=Black] [/color] [color=Black]I will continue adding to this over time as i lean new stuff.[/color] [color=Black] Chageing weapon accuracy [color=Red]-By DeathGiver [/color][/color] [color=Red]I have discovered how you can change the accuracy of a weapon. Very simple really, simply open up your weapon_name.cpp file, and find the text called [/color][color=Red][color=Blue]GetBulletSpread[/color] (use Quick Find to make it easier). You should then see something like this:[/color] virtual const Vector& GetBulletSpread( void ) { // Handle NPCs first static Vector npcCone = VECTOR_CONE_5DEGREES; if ( GetOwner() && GetOwner()->IsNPC() ) return npcCone; static Vector cone; if ( pistol_use_new_accuracy.GetBool() ) { float ramp = RemapValClamped( m_flAccuracyPenalty, 0.0f, PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME, 0.0f, 1.0f ); [color=SeaGreen]//We lerp from very accurate to inaccurate over time[/color] VectorLerp( VECTOR_CONE_1DEGREES, VECTOR_CONE_6DEGREES, ramp, cone ); } else { // Old value cone = VECTOR_CONE_4DEGREES; } return cone; } [color=Red] This is what you want to change: [/color] [color=SeaGreen] // We lerp from very accurate to inaccurate over time[/color] VectorLerp( VECTOR_CONE_1DEGREES, VECTOR_CONE_6DEGREES, ramp, cone ); [color=Red] Simply change the [color=Blue]VECTOR_CONE_#DEGREES[/color] entries to whatever degree you desire. The lower the numbers, the more accurate the weapon shoots.[/color] [color=Red] For a static vector cone, the process is only slightly different:[/color] virtualconst Vector& GetBulletSpread( void ) { staticconst Vector cone = VECTOR_CONE_5DEGREES; return cone; } [color=Red]Just change the [color=Blue]VECTOR_CONE_#DEGREES[/color] to whatever you want. Again, the lower the number, the more accurate it shoots.[/color] [color=Black] [/color]




fulac

I don't spend enough time here

50 XP

12th December 2004

0 Uploads

24 Posts

0 Threads

#2 13 years ago

great tutorial, this has to be stickied you gonna do any more??smilie.gif




metal_militia

Killing is my business...

50 XP

29th November 2004

0 Uploads

1,759 Posts

0 Threads

#3 13 years ago

[color=Black]When i tried adding more to the original post it fucked up so i think i might be getting close to the max characters allowed. Anyway, heres anything that coudn't fit in the top post. [/color][color=Black]Fun guns you might want to try [/color] [color=Black]First off one my my personal favourites... [/color][color=Black] The full auto RPG This is veyr easy to mod the RPG into. Of course you can make it as a new gun but for the sake of lazyness here is what you need to change. [/color]

  • [color=Black]Find this section of code in weapon_rpg.cpp[/color]

[color=Black][color=Blue] void CWeaponRPG::PrimaryAttack( void ) { //Can't have an active missile out if ( m_hMissile != NULL ) return; // Can't be reloading if ( GetActivity() == ACT_VM_RELOAD ) return; Vector vecOrigin; Vector vecForward; m_flNextPrimaryAttack = gpGlobals->curtime + 0.5f;[/color] [/color]

  • Now modify it to this...

[color=Green]void CWeaponRPG::PrimaryAttack( void ) { // Can't have an active missile out //if ( m_hMissile != NULL ) // return; // Can't be reloading if ( GetActivity() == ACT_VM_RELOAD ) return; Vector vecOrigin; Vector vecForward; m_flNextPrimaryAttack = gpGlobals->curtime + 0.1f; [color=Red][color=Black]As you can see all i did was comment out [/color] [/color][/color][color=Green] [color=Blue]if ( m_hMissile != NULL ) return; [color=Red][color=Black]And changed[/color] [color=Blue]curtime + 0.5f;[color=Black] [/color][color=Black]to[/color] [/color][/color][/color][/color][color=Green][color=Blue][color=Red][color=Blue]curtime + 0.1f; [color=Black]This basically means it can have more than one active missle out and a higher refire rate ( i think). fullautorpg_3.jpgfullautorpg_2.jpgfullautorpg_3.jpg One modification i plan to make is to make an airstrike weapon/ laser designator. It will work by spawning about 20 missiles quite far above the player. and not having fireing animations. Mabey just a binoculars model. Shoudl be quite tricky but im fairly certain its possible. [/color][/color][/color][/color][/color] [color=Black] -------------------------------------------------------------------------------------------------- [/color] [color=Black][/color]

[color=Black]C++ Basics[/color]
[color=Black]([color=Red]You can miss this bit out if you want. Its not very relevant but useful if you have any interest in learning C++[/color])[/color]

[color=Black]Explaining how to use C++ is somewhat beyond the scope of this tutorial. If you want that try college. So il just tell you some very basic things about it. hmm where to start. [color=Blue]Variables[/color] Variables are a corner stone of any programming lanugage. These are like temporary stores for data to be held. Creating one allows the computer to store a number, character, string of characters, decimal in the computers memory. Which is, of course, essential if you want anything to happen. For example Rocket speed might be stored in an 'integer' variable. This would be dimentioned by a piece of code like int rocketspeed; rocketspeed = 100; Common variable types: Bool [color=Green]//Boolean value (true/false)[/color] Char [color=Green]//Character (a)[/color] int [color=Green]//Integer (25)[/color] Double [color=Green]//Double precision floating point (1.5)[/color] Long [color=Green]//Long Integer (10000000000L) [color=Black]Additional parts are somtimes added to variables, these provide some useful functions. A commmon one you may see in the HL2 code is.. Const [color=SeaGreen]//Constant. Cannot be modified after the initial number is defined.[/color] For example Const Double value =1.5; [color=SeaGreen]//This code would create a double precision floating point variable with an unchangeable value of 1.5 [color=Blue]Operators [/color][color=Blue][color=Black]Operators is basically a fancy word for.. uhh.. the things you use in maths. They are used to add, multiply etc two variables or values. The most common are as follows: + [color=Green]//Plus[/color] - [color=Green]//Minus[/color] * [color=Green]//Multiply[/color] / [color=Green]//Divide[/color] % [color=Green]//Remainder [color=Black]For example... If weapon fires WeaponClip = WeaponClip -1 [color=Green]//Pseudo code. Not how it would be coded but showing the use of a minus operator. As a side note i believe you can shorten code such as this by using WeaponClip --1. [/color][color=Blue] Comparision operators [color=Black]These are used to compare to variables or values. They are as follows: == [color=Green]//Is equal to[/color] != [color=Green]//Is not equal to[/color] < [color=Green]//Is less than[/color] > [color=Green]//Is greater than [/color] <= [color=Green]//Is less than or equal to[/color] >= [color=Green]//Is greater than or equal to[/color] They may be used in a context such as this... If WeaponClip <= 0 [color=Green]//If the weapon clip is empty...[/color] WeaponClip = 30 [color=Green]//reload clip[/color] Play Reload animation [color=Green]//play reloading animation [color=Black](Again, pseudo type code. Real stuff would be much different)[/color] [/color][/color][/color][/color][/color] [/color][/color][/color][/color][/color] [color=Blue]Comments [/color][color=Blue][color=Black]Comments are useful to know about as they are are fairly frequent in the source. They serve as a way for a human to better understand the code, which is important if another person needs to work on it. They are completely ignored by the program when it is running. i.e. they are not executed in any way. You will be able to tell if something is a comment as it will be green text. To create a comment yourself you simple add // infront of the text. e.g. [color=SeaGreen][color=Black]for ( int i = 0; i < 6; i++ )[/color] //FIX ME: Theres been a fuck-up >: [ [color=Black]This is useful for identifying specific bits of code or simply removeing some code from the program without deleteing it. e.g. if you are having compile errors and want to test if a certain piece of code is causeing the problem you can comment it out and re-compile. to check if it works without said code. [color=Red] Uhh, i think thats all i'll do. Hope ya found this useful and i expect i will update it with some HL2 specific info in the near future.[/color][/color][/color][/color][/color][/color]




Guest

I didn't make it!

0 XP

 
#4 13 years ago

Oh that was a nice job, thanks ;) really, I learned much stuff too :) besides the gun making too :D for example "HOW TO USE VISUAL STUDIO" XD oh goosh, but, thanks, thanks!




metal_militia

Killing is my business...

50 XP

29th November 2004

0 Uploads

1,759 Posts

0 Threads

#5 13 years ago

Tutorial updated.




Guest

I didn't make it!

0 XP

 
#6 13 years ago

I cannot find the folder "scripts" in my mod directory. I did a search on the whole folder and found only 2 .txt files. please help




metal_militia

Killing is my business...

50 XP

29th November 2004

0 Uploads

1,759 Posts

0 Threads

#7 13 years ago

Try

...\Steam\SteamApps\Sourcemods\[your mod name]\scripts

They are not in your src folder so only show up after you compile the mod.




Toprem

I don't spend enough time here

50 XP

8th December 2004

0 Uploads

18 Posts

0 Threads

#8 13 years ago

How do you load the mod after you are done making changes? I really wanna try and make the full auto crossbow like you posted in the other thread, but as Ive never done anything like this, I am slightly clueless on how to do it :O/




metal_militia

Killing is my business...

50 XP

29th November 2004

0 Uploads

1,759 Posts

0 Threads

#9 13 years ago

should be in your steam games list under 3rd party games. Start with creating a simple gun and il add a short tutorial for the crossbow a bit later.




Toprem

I don't spend enough time here

50 XP

8th December 2004

0 Uploads

18 Posts

0 Threads

#10 13 years ago

Hrm, I did what it said for the full auto RPG, but it doesnt seem to work...