Post tutorial RSS How to make a laser weapon (hl2 mod)

If u want to put a laser in your mod just follow this tutorial.

Posted by on - Intermediate Server Side Coding

How to make a the pistol shoot with a laser: Laser Pistol!

Just follow this tutorial and ur pistol is going to shoot laserbeams

1 Open pistol.cpp

add this under includes "gamestat.h":

#include "beam_shared.h"
#include "ammodef.h"
#define PHYSCANNON_BEAM_SPRITE "sprites/orangelight1.vmt"

beam_shared.h defines alot about beams
ammodef.h is needed for the trace of the laser
the PHYSCANNON_BEAM_SPRITE defines what sprite is used, if u change the vmt u can make the laser have another color

add in public (under DECLARE_SERVERCLASS();):

void    DrawBeam( const Vector &startPos, const Vector &endPos, float width );
void    DoImpactEffect( trace_t &tr, int nDamageType );

DrawBeam and DoImpactEffect are new parts that we will put in the script

Add in private (under DECLARE_ACTTABLE();):

int        m_nBulletType;

m_nBulletType is a variable that we will use in the script

Add under BEGIN_DATADESC( CWeaponPistol ):

DEFINE_FIELD( m_nBulletType,            FIELD_INTEGER ),

an extra define for m_nBulletType

We will need trace_t &tr, int nDamageType, CBaseCombatCharacter *pOperator in the Primaryattack part

Go to

CWeaponPistol::CWeaponPistol( void )

Add:

m_nBulletType        = -1;

Scroll down to the end of the script, add there:

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &startPos - 
//			&endPos - 
//			width - 
//			useMuzzle - 
//-----------------------------------------------------------------------------
void CWeaponPistol::DrawBeam( const Vector &startPos, const Vector &endPos, float width )
{
	//Tracer down the middle
	UTIL_Tracer( startPos, endPos, 0, TRACER_DONT_USE_ATTACHMENT, 6500, false, "GaussTracer" );

	//Draw the main beam shaft
	CBeam *pBeam = CBeam::BeamCreate( PHYSCANNON_BEAM_SPRITE, 15.5 );
	
	pBeam->SetStartPos( startPos );
	pBeam->PointEntInit( endPos, this );
	pBeam->SetEndAttachment( LookupAttachment("Muzzle") );
	pBeam->SetWidth( width );
//	pBeam->SetEndWidth( 0.05f );
	pBeam->SetBrightness( 255 );
	pBeam->SetColor( 255, 185+random->RandomInt( -16, 16 ), 40 );
	pBeam->RelinkBeam();
	pBeam->LiveForTime( 0.1f );
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : &tr - 
//			nDamageType - 
//-----------------------------------------------------------------------------
void CWeaponPistol::DoImpactEffect( trace_t &tr, int nDamageType )
{

	//Draw our beam
	DrawBeam( tr.startpos, tr.endpos, 15.5 );

	if ( (tr.surface.flags & SURF_SKY) == false )
	{
		CPVSFilter filter( tr.endpos );
		te->GaussExplosion( filter, 0.0f, tr.endpos, tr.plane.normal, 0 );

		m_nBulletType = GetAmmoDef()->Index("GaussEnergy");

		UTIL_ImpactTrace( &tr, m_nBulletType );
	}
}

We define the beam

2 Open hl2_gamerules.cpp

Change

def.AddAmmoType("Pistol",    DMG_BULLET,    TRACER_LINE_AND_WHIZ,    "sk_plr_dmg_pistol",    "sk_npc_dmg_pistol",    "sk_max_pistol",    BULLET_IMPULSE(200, 1225), 0 );

to

def.AddAmmoType("Pistol",    DMG_DISSOLVE,    TRACER_NONE,        "sk_plr_dmg_pistol",    "sk_npc_dmg_pistol",    "sk_max_pistol",    BULLET_IMPULSE(200, 1225), 0 );

If the enemy dies, he will dissolve and there dont draw a line after the bullet

3 Open \Steam\steamapps\SourceMods\MyMod\scripts\weapon_pistol.txt

Change

"printname"    "#HL2_Pistol"

to

"printname"    "LASER PISTOL"

We change the name in the bucket

If u know how u can change the SoundData so it will make a better noise

4 Open \Steam\steamapps\SourceMods\MyMod\cfg\skill.cfg
Change

sk_plr_dmg_pistol        "5"
sk_npc_dmg_pistol        "3"
sk_max_pistol            "148"


to

sk_plr_dmg_pistol        "20"
sk_npc_dmg_pistol        "15"
sk_max_pistol            "148"

This weapon doesn't shoot with normal bullets but with a laser so it will do a bigger damage to the enemy.

END

I hope this helps u a bit

Speedlly (Scubic)

=> this weapon (or a part of it) is going to play a role in the game that were maken (Cubic Life)

This works... i tested it myself

Comments
ghostings
ghostings

GAH D:

That much work for a single laser pistol?

Reply Good karma Bad karma+2 votes
AlekZanDer
AlekZanDer

That's normal.

Reply Good karma Bad karma+1 vote
Maxen1416
Maxen1416

too much to read,
why dont you upload pre-made files?
its just an idea though.

Reply Good karma Bad karma+2 votes
tymaxbeta
tymaxbeta

that takes all the fun ;D nice tutorial guys, this is going to be amazing :D

Reply Good karma Bad karma+1 vote
cW#Ravenblood

lol thats epic

Reply Good karma Bad karma+1 vote
Calibur25
Calibur25

Just wondering how long it took you to find the code to extend as well as tweek it to your liking? Nice work!

Reply Good karma Bad karma+2 votes
Savant7
Savant7

could be useful for my mod i will pass it forward, thanks!

Reply Good karma Bad karma+1 vote
thehunt
thehunt

Has anyone actually gotten this to work? My laser is being created and it looks way cool, but it's not disappearing...
The code calls the LiveForTime( 0.1f ) method and the 2 statements inside:
SetThink(&CBeam::SUB_Remove);
SetNextThink( gpGlobals->curtime + time );

...but nothing happens. If I shoot 10 times, I have 10 lasers shooting out but they don't go away.

Reply Good karma Bad karma+1 vote
Speedlly Author
Speedlly

It worked for me... i used this code alot...
I have no idea what u did wrong
This code comes from the gun on the jeep so it worked there to...

Reply Good karma+1 vote
Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.