Readding the Physics Gun to your mod

From Valve Developer Community
Jump to navigation Jump to search
Physics gun in Gabe Mod. The result of the tutorial will be more like the physics gun from older versions of JBMod than this one.

In this tutorial, you will learn how to readd the physics gun to your Half-Life 2 modification. This will work on all branches of Source.

Note: This guide somewhat owes a credit to TheMaster974 for Valve Source Code 2013 Tutorial (Episode 2): Fixing the Physgun, which can be found here: https://www.youtube.com/watch?v=utUc71lEm8c

Getting Started

  • Create a mod under Source 2006, Source 2007, or Source 2013.
  • Open up the mod's source code in the respective version of Visual Studio.

Files

In Visual Studio, right click on a project, hover over Add, and select Add Existing Item to add existing files to projects.

  • Add the existing file physgun.cpp to the server project.
  • Add the existing file c_weapon_gravitygun.cpp to the client project.

Code Fixes

If you try compiling the code now, you will get a few errors. We must fix up the code so that it compiles correctly.

Starting with c_weapon_gravitygun.cpp:

In the C_BeamQuadratic class, under virtual int DrawModel( int flags );, Add this:

matrix3x4_t z;
const matrix3x4_t& RenderableToWorldTransform() { return z; }

At the end of the file, change

materials->Bind( pMat )

to Note: In Source 2006, do NOT apply this part of the fix.

CMatRenderContextPtr pRenderContext(materials);
pRenderContext->Bind( pMat );

And we are finished with the first file.

physgun.cpp:

On line 49:

SetModelName( MAKE_STRING( "models/weapons/glueblob.mdl" ) );

Source mods do not have this model by default. If needed, change this model to models/weapons/w_bugbait.mdl.

On line 843: Press Enter to make a new line, type

NetworkStateChanged();

in that line.

Starting on line 1427 to the end of the file: comment that whole section out because it is already defined in util.h. It should start with #define NUM_COLLISION_TESTS 2500.

Compile the code.

Weapon Data File

In your mod's scripts folder, create a new text file titled weapon_physgun.txt. In that file, paste this:

// Physics Gun

WeaponData
{
	// Weapon data is loaded by both the Game and Client DLLs.
	"printname"	"PHYSICS GUN"
	"viewmodel"			"models/weapons/v_physcannon.mdl"
	"playermodel"			"models/weapons/w_Physics.mdl"
	"anim_prefix"			"physgun"
	"bucket"				"0"
	"bucket_position"		"3"

	"clip_size"				"1"
	"clip2_size"			"8"
	"primary_ammo"			"None"
	"secondary_ammo"		"Gravity"
	"default_clip2"			"8"

	"weight"				"0"
	"item_flags"			"2"

	SoundData
	{
		"single_shot"		"Weapon_Physgun.On"
		"reload"			"Weapon_Physgun.Off"
		"special1"			"Weapon_Physgun.Special1"
	}

	// Weapon Sprite data is loaded by the Client DLL.
	TextureData
	{
		"weapon"
		{
				"font"		"WeaponIcons"
				"character"	"m"
		}
		"weapon_s"
		{	
				"font"		"WeaponIconsSelected"
				"character"	"m"
		}
		"ammo"
		{
			"file"		"sprites/a_icons1"
			"x"			"0"
			"y"			"0"
			"width"		"32"
			"height"	"32"
		}
		"crosshair"
		{
			"file"		"sprites/crosshairs"
			"x"			"0"
			"y"			"48"
			"width"		"24"
			"height"	"24"
		}
		"autoaim"
		{
				"file"		"sprites/crosshairs"
				"x"			"48"
				"y"			"72"
				"width"		"24"
				"height"	"24"
		}
	}
}

In-Game

  1. Open the game.
  2. Load into a map.
  3. Type into the console: give weapon_physgun.
  4. That's it!

Controls: Primary attack moves objects around with a beam. Secondary attack places sticky pellets to weld objects together.