Manual shotgun pumping: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Removed the video, if someone wants to record a more prompt and better one, feel free.)
m (clean up, added orphan, deadend tags)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
I recently modified the shotgun to be more like a manual pump shotgun rather than shoot, auto pump with animations. It's a one-liner so it wasn't difficult, but a really cool outcome (at least I thought so). Anyway here goes:
{{Multiple issues|
{{Dead end|date=January 2024}}
{{Orphan|date=January 2024}}
}}


Obviously, I recommend [http://developer.valvesoftware.com/wiki/Using_Subversion_for_Source_Control_with_the_Source_SDK Subversion] if you haven't already created a repository, go ahead and do that. That's out of the scope of the tutorial but see wiki.
Modifying the shotgun to be more like a pump action shotgun is not difficult, but has a really cool outcome.


Open weapon_shotgun.cpp
== Requirements ==
* A repository for storing your code changes. (GitHub, Subversion, etc)
* Visual Studio for changing and compiling your code.


Now, first thing I did was take out auto reload if the user runs out of bullets and forgets to reload, too bad.
== Tutorial ==


Should be about line 543 (645 in newer versions of the Source SDK).
Open {{file|weapon_shotgun.cpp}}
 
'''Step One''': Remove auto reload
 
Look for line 543 (Line 645 in newer versions of the Source SDK) and comment it out using comment block (/* ... */).
<pre>else
<pre>else
{
{
Line 21: Line 30:
}</pre>
}</pre>


Comment it out using comment block (/* ... */) instead of the single line (//).
Modified Code:
 
<pre>/*
<pre>/*
else
else
Line 37: Line 45:
}*/</pre>
}*/</pre>


Now our shotgun will not automatically reload if all bullets are expended. If all the bullets are expended, do nothing is basically what we are doing here. You could have also looked at ITEM_FLAG_NOAUTORELOAD, but hey, why do that when you have comments? Matter of implementation.  This obviously requires less work and less chance of messing something else up. However, you leave code in your source which adds a small amount of bloating of the code (minuscule in size).
Now, the shotgun will not automatically reload if all bullets are expended. You could have also looked at <code>ITEM_FLAG_NOAUTORELOAD</code>, but this requires less work and has a smaller chance of messing something up. However, you will be leaving code in your source which adds a small amount of bloat (although minuscule in size).


Let's get to the nitty gritty. Making a real pump action shotty.
'''Step 2''': Making the user manually pump the shotgun.
Scroll to line 446 (547 in newer versions of the Source SDK). Change this:


Scroll to line 446 (Line 547 in newer versions of the Source SDK).
Change this code block:
<pre>if ((m_bNeedPump) && (m_flNextPrimaryAttack <= gpGlobals->curtime))
<pre>if ((m_bNeedPump) && (m_flNextPrimaryAttack <= gpGlobals->curtime))
{
{
Line 55: Line 65:
}</pre>
}</pre>


Analyze this for a minute, that way we are learning something instead of copy paste coding. Realize that the compiler follows PEMDAS (Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction), order of looking at 'if' statements, so a statement inside a parenthesis is going to be analyzed first.
Analyze this code for a minute. The compiler follows {{wiki|PEMDAS}} (Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction) when looking at 'if' statements, so a statement inside a parenthesis is going to be analyzed first.
 
Original 'if' statement works like this: If you need to pump the shotgun, and the next attack time is less than or equal to the current time, pump the gun.


First 'if' statement says this: If you need to pump the shotty, and the next attack time is less than or equal to the current time, pump the gun.
The changes work like this: If the user is pressing the +attack or +attack2 button ''and'' you need to pump ''and'' attack time is less than or equal to current time, pump the shotgun. Basically, this logic is what is making the gun into a manual action.
Our change says this: If the user is pressing the attack or attack2 button and you need to pump and attack time is less than or equal to current time, let's pump.


Pretty important you understand that logic. Basically, this logic is what is making our gun more of a manual action.
Now, if you wanted the user to be able to reload only if you expend all their ammunition, because there is an expended round in the chamber they would need to pump it out.


Now, I also wanted to be able to pump after you reload ONLY if you expend all your ammunition. This is because if you expend all your ammo now, you want to reload immediately, but you will have an expended round in the chamber, so pump it out.
Scroll up to line 146 (line 246 in newer versions) (Inside bool CWeaponShotgun::StartReload( void ))
Scroll up to line 146(246 in newer versions) (inside bool CWeaponShotgun::StartReload( void ))and <b>after</b>:


'''after''':
<pre>if (j <= 0)
<pre>if (j <= 0)
return false;
return false;
Line 76: Line 87:
}</pre>
}</pre>


Compile, load up, enjoy.  Any problems send me a PM to otisranson on Steam forums, or send me an email OtisRanson2@gmail.com.
Compile, load up, enjoy.  Any problems send me a PM to otisranson on Steam, or send an email to OtisRanson2@gmail.com.


[[Category:Free source code]]
[[Category:Free source code]]
[[Category:Tutorials]]

Latest revision as of 10:07, 21 January 2024

Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

Modifying the shotgun to be more like a pump action shotgun is not difficult, but has a really cool outcome.

Requirements

  • A repository for storing your code changes. (GitHub, Subversion, etc)
  • Visual Studio for changing and compiling your code.

Tutorial

Open 🖿weapon_shotgun.cpp

Step One: Remove auto reload

Look for line 543 (Line 645 in newer versions of the Source SDK) and comment it out using comment block (/* ... */).

else
{
	// weapon is useable. Reload if empty and weapon has waited as long as it has to after firing
	if ( m_iClip1 <= 0 && !(GetWeaponFlags() & ITEM_FLAG_NOAUTORELOAD) && m_flNextPrimaryAttack < gpGlobals->curtime )
	{
		if (StartReload())
		{
			// if we've successfully started to reload, we're done
			return;
		}
	}
}

Modified Code:

/*
else
{
	// weapon is useable. Reload if empty and weapon has waited as long as it has to after firing
	if ( m_iClip1 <= 0 && !(GetWeaponFlags() & ITEM_FLAG_NOAUTORELOAD) && m_flNextPrimaryAttack < gpGlobals->curtime )
	{
		if (StartReload())
		{
			// if we've successfully started to reload, we're done
			return;
		}
	}
}*/

Now, the shotgun will not automatically reload if all bullets are expended. You could have also looked at ITEM_FLAG_NOAUTORELOAD, but this requires less work and has a smaller chance of messing something up. However, you will be leaving code in your source which adds a small amount of bloat (although minuscule in size).

Step 2: Making the user manually pump the shotgun.

Scroll to line 446 (Line 547 in newer versions of the Source SDK).

Change this code block:

if ((m_bNeedPump) && (m_flNextPrimaryAttack <= gpGlobals->curtime))
{
	Pump();
	return;
}

To this:

if ((m_bNeedPump) && (m_flNextPrimaryAttack <= gpGlobals->curtime) && (pOwner->m_nButtons & IN_ATTACK || pOwner->m_nButtons & IN_ATTACK2))
{
	Pump();
	return;
}

Analyze this code for a minute. The compiler follows Wikipedia icon PEMDAS (Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction) when looking at 'if' statements, so a statement inside a parenthesis is going to be analyzed first.

Original 'if' statement works like this: If you need to pump the shotgun, and the next attack time is less than or equal to the current time, pump the gun.

The changes work like this: If the user is pressing the +attack or +attack2 button and you need to pump and attack time is less than or equal to current time, pump the shotgun. Basically, this logic is what is making the gun into a manual action.

Now, if you wanted the user to be able to reload only if you expend all their ammunition, because there is an expended round in the chamber they would need to pump it out.

Scroll up to line 146 (line 246 in newer versions) (Inside bool CWeaponShotgun::StartReload( void ))

after:

if (j <= 0)
	return false;

Insert this:

if( m_iClip1 == 0 )
{
	m_bNeedPump = true ;
}

Compile, load up, enjoy. Any problems send me a PM to otisranson on Steam, or send an email to OtisRanson2@gmail.com.