Wavelength Logo
tl.jpg (2351 bytes) blank.gif (837 bytes) tr.jpg (2446 bytes)
blank.gif (837 bytes)
Basic Weapon Reference Code by BigGuy
blank.gif (837 bytes)
(This assumes you have MS VC++ 5.0 or better, if you dont have these then I will be unable to help you compile your dll.)

Another tutorial by BigGuy, here are the basic things you need in a normal weapon. These may need to be modified for different weapons, melee for example. I figure this will be a good example for those who are just starting out. This is for absolute beginners. If you do have trouble with this you can email me about it.
NOTE: This tutorial will not compile, it is only an example of the basics of weapon coding.


YourWeapon.cpp
This is the file that makes up your weapon.

////Default #include statements\\\\

#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "weapons.h"
#include "nodes.h"
#include "player.h"
#include "gamerules.h"

////Extra #include for special effects: fading, shaking, other effects\\\\

#include "shake.h"

////Model animations\\\\
enum modelname_e {
model_anim1,
model_anim2,
model_anim3,
model_anim4,
model_anim5,
model_anim6
}

////Weapon Class\\\\
class CWeaponName: public CBasePlayerWeapon
{
public:
void Spawn (void);
void Precache (void);

int iItemSlot( void ) { return 1; } //inline function!
int GetItemInfo(ItemInfo *p);
int AddToPlayer( CBasePlayer *pPlayer );

void PrimaryAttack( void );
void SecondaryAttack(void);

BOOL Deploy( void );
void Holster( void );
void WeaponIdle( void );
////Declare your extra variables here\\\\
/* Example:
int zoom;
*/
}
////these need to be included in every weapon, but some special cases do occur\\\\

/*//this is important to add if you plan to put the new weapon in a level but im not going into that\\*\

LINK_ENTITY_TO_CLASS (weapon_name, CWeaponName)

////Now we are going to setup are functions\\\\

////This function is called when the weapon is first dropped by player or world\\\\
void CWeaponName::Spawn(void) //MSVC makes this easier for us.
{
////I would set your first use variables here\\\\
Precache( ); //call precache
m_iId = WEAPON_NAME; //this will be the name defined in weapons.h
SET_MODEL(ENT(pev), "models/w_modelname"); //fill in your weapons model
m_iClip = DEFAULT_AMMO; //gives default ammo defined in weapons.h

FallInit();// get ready to fall down.


}

////Stores your weapons models, sounds and other files in RAM before use\\\\
void CWeaponName::Precache(void)
{
/* it would be impossible for me to predict what precaches you need so heres an example:
PRECACHE_MODEL("models/w_egon.mdl");
PRECACHE_MODEL("models/v_egon.mdl");
PRECACHE_MODEL("models/p_egon.mdl");

PRECACHE_MODEL("models/w_9mmclip.mdl");
PRECACHE_SOUND("items/9mmclip1.wav");

PRECACHE_SOUND( RGUN_SOUND_OFF );
PRECACHE_SOUND( RGUN_SOUND_RUN );
PRECACHE_SOUND( RGUN_SOUND_STARTUP );

PRECACHE_MODEL( RGUN_BEAM_SPRITE );
PRECACHE_MODEL( RGUN_FLARE_SPRITE );

PRECACHE_SOUND ("weapons/357_cock1.wav");
*/
}

////vital weapon info, defined in weapons.h\\\\
int CWeaponName::GetItemInfo(ItemInfo *p)
{/*
p->pszName = STRING(pev->classname);
p->pszAmmo1 = "9mm"; //first type of ammo
p->iMaxAmmo1 = MAX_AMMO_CARRY;
p->pszAmmo2 = "Uranium';
p->iMaxAmmo2 = MAX_AMMO_CARRY;
p->iMaxClip = WEAPONNAME_CLIP;
p->iSlot = WEAPON_SLOT;
p->iPosition = WEAPON_SLOTPOSITION;
p->iId = WEAPON_NAME;
p->iWeight = WEAPONNAME_WEIGHT;
return 1;
*/
}//Here again I can only give you an example

////Add your weapon to player\\\\
int CWeaponName::AddToPlayer( CBasePlayer *pPlayer )
{
if ( CBasePlayerWeapon::AddToPlayer( pPlayer ) )
{
MESSAGE_BEGIN( MSG_ONE, gmsgWeapPickup, NULL, pPlayer->pev );
WRITE_BYTE( m_iId );
MESSAGE_END();
return TRUE;
}
return FALSE;
}//this is pretty much the same on all weapons

////Deploy your weapon\\\\
////set your variables here\\\\
BOOL CWeaponName::Deploy (void)
{
return DefaultDeploy( "v_model.mdl", "p_model.mdl", model_anim1, "WeaponName")
}

////reset your variables here\\\\
////cancel reloads, fades, or other effects\\\\
void CWeaponName::Holster(void)
{
m_pPlayer->m_flNextAttack = gpGlobals->time + 0.5; //Same for all
SendWeaponAnim( model_anim2)
}

void CWeaponName::WeaponIdle(void)
{
////Cant help much here other than just look at other weapons\\\\
}

void CWeaponName::PrimaryAttack(void)
{
////this is your weapons main fire (mouse button 1 & enter)\\\\
}

void CWeaponName::SecondaryAttack(void)
{
////this is your weapons second fire (mouse 2)\\\\
////can also be zoom and other effects\\\\
}


Any questions or suggestions should be sent to me:
venturis52@hotmail.com

 

blank.gif (837 bytes)
bl.jpg (2471 bytes) blank.gif (837 bytes) br.jpg (2132 bytes)