Authoring your first weapon entity: Difference between revisions
No edit summary |
No edit summary |
||
Line 126: | Line 126: | ||
#endif | #endif | ||
</pre> | </pre> | ||
[[Category:Weapons programming]] | [[Category:Weapons programming]] |
Revision as of 08:37, 7 May 2017
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); 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(float, m_flRateOfFire); private: CWeaponMyFirstWeapon(const CWeaponMyFirstWeapon &);
Basically, this just creates a variable, with the type of a float, called m_flRateOfFire
, and creates an alternate constructor.
To define m_flRateOfFire
you can type
m_flRateOfFire = 0.4f; // 60 seconds divided by 150 shots (150 rpm) equals to 0.4
The same can also be done at the top of the file, with a #define
.
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