Ads keep us online. Without them, we wouldn't exist. We don't have paywalls or sell mods - we never will. But every month we have large bills and running ads is our only way to cover them. Please consider unblocking us. Thank you from GameBanana <3

Adding Ironsights to guns

A Tutorial for Half-Life 2

Members see zero ads. Membership is 100% free
[title]Adding Ironsights to guns[/title] [subtitle]Basic knowledge of C++ is needed[/subtitle] [u]hl2mp_weapon_parse.h (DM) or weapon_parse.h (Singleplayer) Add this in the bottom of the file. Only add the lines with //ADDED behind.[/u] public: //jorg40 - ironsight Vector m_expOffset; //ADDED QAngle m_expOriOffset; //ADDED int m_iPlayerDamage; }; hl2mp_weapon_parse.cpp (DM) or weapon_parse.cpp (Singleplayer) [u]Add this in the file.[/u] void CHL2MPSWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName ) { ... // this just saves off the data in the script file for later use KeyValues *pEt = pKeyValuesData->FindKey("ExpOffset"); if (pEt) { m_expOffset.x = pEt->GetFloat("x", 0.0f); m_expOffset.y = pEt->GetFloat("y", 0.0f); m_expOffset.z = pEt->GetFloat("z", 0.0f); m_expOriOffset.x = pEt->GetFloat("xori", 0.0f); m_expOriOffset.y = pEt->GetFloat("yori", 0.0f); m_expOriOffset.z = pEt->GetFloat("zori", 0.0f); } else { m_expOffset = vec3_origin; m_expOriOffset.Init(); } ... } baseviewmodel_shared.cpp [u]Change in the beginning of the file so it looks like this[/u] #include "cbase.h" #include "baseviewmodel_shared.h" #include "datacache/imdlcache.h" #if defined( CLIENT_DLL ) #include "iprediction.h" #include "prediction.h" // cin: 070105 - ironsight mode changes #include "convar.h" #include "c_hl2mp_player.h" #include "weapon_hl2mpbase.h" #else #include "vguiscreen.h" #endif // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" [u]Then under that add so it should look like this.[/u] //THIS IS ALREADY IN THE CODE, just so you know where we are #define VIEWMODEL_ANIMATION_PARITY_BITS 3 #define SCREEN_OVERLAY_MATERIAL "vgui/screens/vgui_overlay" //ALL BELOW IS NEW #if defined( CLIENT_DLL ) void ExpWpnTestOffset(ConVar *pConVar, char *pszString); ConVar cl_exp_test_wpn_offset("cl_exp_test_wpn_offset", "0", 0, "Tests weapon offsets", (FnChangeCallback_t)ExpWpnTestOffset); ConVar cl_exp_test_wpn_offset_x("cl_exp_test_wpn_offset_x", "0"); ConVar cl_exp_test_wpn_offset_y("cl_exp_test_wpn_offset_y", "0"); ConVar cl_exp_test_wpn_offset_z("cl_exp_test_wpn_offset_z", "0"); ConVar cl_exp_test_wpn_ori_offset_x("cl_exp_test_wpn_ori_offset_x", "0"); ConVar cl_exp_test_wpn_ori_offset_y("cl_exp_test_wpn_ori_offset_y", "0"); ConVar cl_exp_test_wpn_ori_offset_z("cl_exp_test_wpn_ori_offset_z", "0"); // cin: 070105 - applies existing weapon offsets when // entering test mode (this will not be called upon // weapon change, so beware) // this mode should only be used for calibrating the // ironsighted mode offests for a particular weapon void ExpWpnTestOffset(ConVar *pConVar, char *pszString) { CBasePlayer *pPlayer = UTIL_PlayerByIndex(engine->GetLocalPlayer()); if (pPlayer) { CWeaponHL2MPBase *pWeapon = dynamic_cast(pPlayer->GetActiveWeapon()); if (pWeapon) { cl_exp_test_wpn_offset_x.SetValue(pWeapon->GetHL2MPWpnData().m_expOffset.x); cl_exp_test_wpn_offset_y.SetValue(pWeapon->GetHL2MPWpnData().m_expOffset.y); cl_exp_test_wpn_offset_z.SetValue(pWeapon->GetHL2MPWpnData().m_expOffset.z); cl_exp_test_wpn_ori_offset_x.SetValue(pWeapon->GetHL2MPWpnData().m_expOriOffset.x); cl_exp_test_wpn_ori_offset_y.SetValue(pWeapon->GetHL2MPWpnData().m_expOriOffset.y); cl_exp_test_wpn_ori_offset_z.SetValue(pWeapon->GetHL2MPWpnData().m_expOriOffset.z); } } } // last time ironsighted mode was toggled float gIronsightedTime(0.0f); // I bound this to a key for testing(i.e. bind [ ironsight_toggle) CON_COMMAND(ironsight_toggle, "toggles ironsight mode for the current weapon") { if (gpGlobals->curtime - gIronsightedTime < 0.5f) return; CBasePlayer *pPlayer = UTIL_PlayerByIndex(engine->GetLocalPlayer()); if (pPlayer) { C_BaseViewModel *pVm = pPlayer->GetViewModel(); if (pVm) { //pPlayer->m_Local.m_iHideHUD |= HIDEHUD_CROSSHAIR;) pVm->m_bExpSighted ^= true; gIronsightedTime = gpGlobals->curtime; } } } void CalcExpWpnOffsets(CBasePlayer *owner, Vector &pos, QAngle &ang) { Vector forward, right, up, offset; // this is a simple test mode to help determine the proper values // to place in the weapon script if (cl_exp_test_wpn_offset.GetBool()) { ang.x += cl_exp_test_wpn_ori_offset_x.GetFloat(); ang.y += cl_exp_test_wpn_ori_offset_y.GetFloat(); ang.z += cl_exp_test_wpn_ori_offset_z.GetFloat(); offset.Init(cl_exp_test_wpn_offset_x.GetFloat(), cl_exp_test_wpn_offset_y.GetFloat(), cl_exp_test_wpn_offset_z.GetFloat()); } else { CWeaponHL2MPBase *pWeapon = dynamic_cast(ToHL2MPPlayer(owner)->GetActiveWeapon()); if (pWeapon) { ang += pWeapon->GetHL2MPWpnData().m_expOriOffset; offset = pWeapon->GetHL2MPWpnData().m_expOffset; } } // get eye direction angles AngleVectors(ang, &forward, &right, &up); // apply the offsets pos += forward * offset.x; pos += right * offset.y; pos += up * offset.z; } #endif [u]Scroll down some and you will find the ViewModel::Spawn Edit it to look like this:[/u] void CBaseViewModel::Spawn( void ) { Precache( ); SetSize( Vector( -8, -4, -2), Vector(8, 4, 2) ); SetSolid( SOLID_NONE ); #ifdef CLIENT_DLL // cin: 070105 - ironsighted mode changes m_bExpSighted = false; m_expFactor = 0.0f; gIronsightedTime = 0.0f; #endif } [u]Animating the gun[/u] [u]Now while we are still in the file we need some more. Look for void CBaseViewModel::CalcViewModelView. Replace the whole function with this.[/u] void CBaseViewModel::CalcViewModelView( CBasePlayer *owner, const Vector& eyePosition, const QAngle& eyeAngles ) { // UNDONE: Calc this on the server? Disabled for now as it seems unnecessary to have this info on the server #if defined( CLIENT_DLL ) QAngle vmangoriginal = eyeAngles; QAngle vmangles = eyeAngles; Vector vmorigin = eyePosition; CBaseCombatWeapon *pWeapon = m_hWeapon.Get(); //Allow weapon lagging if ( pWeapon != NULL ) { #if defined( CLIENT_DLL ) if ( !prediction->InPrediction() ) #endif { pWeapon->AddViewmodelBob( this, vmorigin, vmangles ); CalcViewModelLag( vmorigin, vmangles, vmangoriginal ); } } #if defined( CLIENT_DLL ) if ( !prediction->InPrediction() ) { // Let the viewmodel shake at about 10% of the amplitude of the player's view vieweffects->ApplyShake( vmorigin, vmangles, 0.1 ); } #endif // cin: 070105 - ironsighted mode changes // get the wpn offsets CalcExpWpnOffsets(owner, vmorigin, vmangles); // get delta time for 1 sec interpolation and interpolate to/from positional offset float delta(gpGlobals->curtime - gIronsightedTime); m_expFactor = (m_bExpSighted) ? (delta > 1.0f) ? 1.0f : delta : (delta > 1.0f) ? 0.0f : 1.0f - delta; Vector difPos(vmorigin - eyePosition); vmorigin = eyePosition + (difPos * m_expFactor); SetLocalOrigin(vmorigin); SetLocalAngles(vmangles); #endif } [u]Looks easy huh? What does it do then? The delta variables says what is what. 1.0 is when the gun is fully ironsighted, and 0.0 says when it is not sighted at all. So this codes calculates the distance between and slide the gun from 0 to 1.[/u] baseviewmodel_shared.h [u]You need to add a couple of public defines here.[/u] class CBaseViewModel : public CBaseAnimating { DECLARE_CLASS( CBaseViewModel, CBaseAnimating ); public: bool m_bExpSighted; //ADDED float m_expFactor; //ADDED DECLARE_NETWORKCLASS(); DECLARE_PREDICTABLE(); #if !defined( CLIENT_DLL ) DECLARE_DATADESC(); #endif [u]Extra Some of you might think the gun is is moving up to the designated point a bit too slow, then here is the solution to it. Nothing hard nothing particular cool stuff, just 2 lines of code. Look for this line: float gIronsightedTime(0.0f); Below it add this. float gMoveTime(0.1f); //Jorg40 - Seconds to use to move the model up to players view Last line. Go to the delta float delta(gpGlobals->curtime - gIronsightedTime); Change it to look like this: float delta((gpGlobals->curtime - gIronsightedTime) / gMoveTime); Done! All we need to do is to change the script files. weapon_pistol.txt Add this somewhere in the script file.[/u] ExpOffset { "x" "-15" "y" "-5.5" "z" "4.2" // "xori" "-2.9" //Does not work // "yori" "1.1" //Does not work // "zori" "2.0" // Does not work } [/]Singleplayer Differences To achieve this in singleplayer make the following alterations:[/u] Replace: CWeaponHL2MPBase *pWeapon = dynamic_cast(pPlayer->GetActiveWeapon()); with CBaseCombatWeapon *pWeapon = pPlayer->GetActiveWeapon(); Replace: CWeaponHL2MPBase *pWeapon = dynamic_cast(ToHL2MPPlayer(owner)->GetActiveWeapon()); with CBaseCombatWeapon *pWeapon = owner->GetActiveWeapon(); Replace: ConVar cl_exp_test_wpn_offset("cl_exp_test_wpn_offset", "0", 0, "Tests weapon offsets", (FnChangeCallback_t)ExpWpnTestOffset); with ConVar cl_exp_test_wpn_offset("cl_exp_test_wpn_offset", "0", 0, "Tests weapon offsets", (FnChangeCallback)ExpWpnTestOffset); Replace: #include "convar.h" #include "c_hl2mp_player.h" #include "weapon_hl2mpbase.h" with #include "convar.h" #include "c_baseplayer.h" Replace: pWeapon->GetHL2MPWpnData() with pWeapon->GetWpnData() [img]http://developer.valvesoftware.com/w/images/6/60/Ironsights_result.jpg[/img]

Comments

Sign up to access this!
1-10 of 23
1
Pages
Go to page:
  • KCrimson avatar
    KCrimson Joined 2y ago
    Offline
    access_time 2y
    Does anyone have a script link for this? I'm a noob and lazy I know. But I just want HL2 ironsights, that's it.
    Bananite
    URL to post:
  • anthonytico avatar
    anthonytico Joined 8y ago
    Offline
    access_time 7y
    **Pros:** - i can t understand please help sorry for my bad english
    Bananite
    URL to post:
  • I don't wanna say i'm lazy but can someone post the final product? i've been looking for this EVERYWHERE
    A shotgun with a penguin. wat?
    URL to post:
  • .:S.W.A.T:. avatar
    .:S.W.A.T:. Joined 11y ago
    Offline
    access_time 10y
    GO_oD I will use it for Custom weapons like berreta ! It will look alike INS mod ! WTF WITH LIFEs ?
    Real-Old SWAT-David avatar
    Mantra
    Real-Old SWAT-David
    URL to post:
  • Hexon avatar
    Hexon Joined 12y ago
    Offline
    1,081 points Ranked 23965th
    access_time 10y
    This is really an example of what-we-must-not-do tutorial. The code is giving to people and they have to make themselves working. It's easy to make tuts for "Basic knowledge of C++ is needed", but couldn't you make a tut for guy who just want to add ironsight to their mod and don't want to spend 6 hours on it to get this crap working ? And this is more bad maded than this one we can found at VDC, I guess it's just took to you 1 min to post it. I'm trying to add this for a month and I always get "undeclared identifier" even if I followed word by word what it was written.
    URL to post:
  • Jorg40 avatar
    Jorg40 Joined 10y ago
    Offline
    access_time 10y
    I just want to input my view here: I was not the one who wrote all of that code, just tiny tiny parts. It was only me who implemented the code I recieved from several expert coders like Cin and Wraiyth and some I picked up along the way accidentaly, and put it into a big/huge chunk of code that eventually would function all together, so all I did was the welding and tweaking part. So I decided to share it with the community. BTW: Check out the video result: http://www.youtube.com/watch?v=BJ0YTHQDx8I&feature=channel_page
    Bananite
    URL to post:
  • Static Frost avatar
    Static Frost Joined 11y ago
    Offline
    771 points Ranked 42169th
    access_time 10y
    Posted by shirk
    Posted by czech_d3v3l0p3r This is the very outdated way of adding ironsights, there's also a new one written by z33ky that is completely up-to-date.
    well can you give me a link to it?
    z33ky's tutorial didn't work for me. I followed it word-for-word and I still got several compiling errors(most to do with undeclared identifiers). I would really prefer too use z33ky's tutorial, because it has more options. Does anybody have a fix for z33ky's tutorial? I would be really greatful if they did.
    Banana Hammock!! avatar
    Mantra
    Banana Hammock!!
    URL to post:
  • Alex' avatar
    Alex' username pic Joined 11y ago
    Sight seeing
    49,326 points Ranked 84th
    27 medals 2 legendary 5 rare
    • 1st Place - Time Traveler Mapping Contest Medal icon
    • 10 years a member Medal icon
    • 6 years a member Medal icon
    • Submitted 5 Apps Medal icon
    • 3rd Place  - 2015 Uberstyles Contest! Medal icon
    • Returned 1000 times Medal icon
    access_time 10y
    You are the one! Thank you for this tutorial
    When Quality Matters avatar
    Mantra
    When Quality Matters
    URL to post:
  • czech_d3v3l0p3r avatar
    czech_d3v3l0p3r Joined 11y ago
    Offline
    access_time 11y
    Posted by shirk well can you give me a link to it?
    Linky
    dragon slayer
    URL to post:
  • Shirk avatar
    Shirk Joined 11y ago
    Offline
    172 points Ranked 57930th
    access_time 11y
    Posted by czech_d3v3l0p3r This is the very outdated way of adding ironsights, there's also a new one written by z33ky that is completely up-to-date.
    well can you give me a link to it?
    Banned
    URL to post:

Embed

menu
Share banner
Image URL
HTML embed code
BB embed code
Markdown embed code

Credits

Key Authors
jorg40
Unknown
shirk
Unknown

Submitter

Guest avatar
Guest Joined 14y ago
Offline
19 medals 4 legendary 6 rare
  • Submitted 100 Skins Medal icon
  • Submitted 50 Maps Medal icon
  • Submitted 200 Threads Medal icon
  • Submitted 30 Tutorials Medal icon
  • Submitted 50 Skins Medal icon
  • Submitted 20 Maps Medal icon
Guest
Creator
Sign up to access this!
Sign up to access this!
Sign up to access this!

Game

Sign up to access this!

Category

Details

Difficulty Level
Advanced

Attributes

Share

  • Share on Reddit
  • Share on Twitter
  • Share on Facebook
  • favorite 3
  • remove_red_eye 25.6k
  • mode_comment 23
  • access_time 11y
  • access_time 8y

More from Submitter

menu

WiPs by Submitter

menu

More Other/Misc Tutorials