Authoring your first weapon entity

From Valve Developer Community
Revision as of 15:57, 7 May 2017 by Atom (talk | contribs) (Creating The Functions)

Jump to: navigation, search

Template:Toc-top

Overview

It should not come as a surprise that the Source engine has a very robust weapon system. This article will cover creating a weapon entity from scratch. Various topics on creating a weapon will be discussed.

Getting Started

To get started, create a new .cpp file in the server project and call it weapon_myfirstweapon. Type this into your newly created file

#include "cbase.h"
#include "npcevent.h"
#include "in_buttons.h"

#ifdef CLIENT_DLL
#include "c_hl2mp_player.h"
#else
#include "hl2mp_player.h"
#endif

#include "weapon_hl2mpbasehlmpcombatweapon.h"

#ifndef CLIENT_DLL
#define CWeaponMyFirstWeapon C_WeaponMyFirstWeapon
#endif

Just to note something here, this tutorial expects the reader is using the Source SDK Multiplayer, but if the reader wanted to make the weapon singleplayer, it should be an easy fix.

Creating The Class

Now we will create the class that contains all the functions, so we can put the code in later. To start, make a new class, and define it like this

class CWeaponMyFirstWeapon : public CBaseHL2MPCombatWeapon

Now, open it up with some curly braces, and type this

public:
  DECLARE_CLASS(CWeaponMyFirstWeapon, CBaseHL2MPCombatWeapon);

  CWeaponMyFirstWeapon(void);

  DECLARE_NETWORKCLASS();
  DECLARE_PREDICTABLE();

The code above just starts off the file, makes so it works with the server, and creates the base for the constructor.

Creating The Functions

After the line that says DEALRE_PREDICTABLE, type these functions in

void Precache(void);
void ItemPreFrame(void);
void ItemBusyFrame(void);
void ItemPostFrame(void);
void PrimaryAttack(void);
void AddViewKick(void);

Activity GetPrimaryAttackActivity(void);

virtual bool Reload(void);

virtual const Vector& GetBulletSpread(void)
{
    static Vector cone = VECTOR_CONE_1DEGREES;
    return cone;
}

#ifndef CLIENT_DLL
    DECLARE_ACTTABLE();
#endif

All this does is create stubs for the functions we will define later, with the exception of GetBulletSpread. To understand all of the functions, see Authoring a weapon entity, but the same things will be described later.

Creating Private Code

In this section, the reader will learn how to create variables, so they can be networked. To start off the private section, type

private:
    CNetworkVar(int, m_iTest);

private:
    CWeaponMyFirstWeapon(const CWeaponMyFirstWeapon &);

Basically, this just creates a variable, with the type of a int, called m_iTest, and then this code creates an alternate constructor. How we will change the value of m_iTest will be discussed later. If your getting an error right at the C, just ignore that. You can also create this same variable at the top, with a #define.

#define m_iTest 1

Just make sure to delete this instance under the private:

Defining The Functions

Now, go out of the braces for the CWeaponMyFirstWeapon class. Next, type

IMPLEMENT_NETWORKCLASS_ALIASED(WeaponMyFirstWeapon, DT_WeaponMyFirstWeapon)

BEGIN_NETWORK_TABLE(CWeaponMyFirstWeapon, DT_WeaponMyFirstWeapon)
#ifndef CLIENT_DLL
RecvPropFloat(RECVINFO(m_flRateOfFire)),
#else
SendPropFloat(SENDINFO(m_flRateOfFire)),
#endif

This code just defines how things will be networked, and what things will be networked. Moving on, to create the weapons entity, type

LINK_ENTITY_TO_CLASS(weapon_myfirstweapon, CWeaponMyFirstWeapon);
PRECACHE_WEAPON_REGISTER(weapon_myfirstweapon); //This defines what the weapon script we make later will be called

Now, to declare the acttable. This is not very important, it just defines what animations to use. For more information, check out the docs on the acttable.

#ifndef CLIENT_DLL
acttable_t CWeaponMyFirstWeapon::m_acttable[] =
{
	{ ACT_HL2MP_IDLE, ACT_HL2MP_IDLE_PISTOL, false },
	{ ACT_HL2MP_RUN, ACT_HL2MP_RUN_PISTOL, false },
	{ ACT_HL2MP_IDLE_CROUCH, ACT_HL2MP_IDLE_CROUCH_PISTOL, false },
	{ ACT_HL2MP_WALK_CROUCH, ACT_HL2MP_WALK_CROUCH_PISTOL, false },
	{ ACT_HL2MP_GESTURE_RANGE_ATTACK, ACT_HL2MP_GESTURE_RANGE_ATTACK_PISTOL, false },
	{ ACT_HL2MP_GESTURE_RELOAD, ACT_HL2MP_GESTURE_RELOAD_PISTOL, false },
	{ ACT_HL2MP_JUMP, ACT_HL2MP_JUMP_PISTOL, false },
	{ ACT_RANGE_ATTACK1, ACT_RANGE_ATTACK_PISTOL, false },
};

IMPLEMENT_ACTTABLE(CWeaponMyFirstWeapon);

#endif