Talk:Adding Ironsights

From Valve Developer Community
Jump to: navigation, search

Source SDK 2013 SP Code (Episodic) bug

There is a problem with this code under the Source SDK 2013 SP (Episodic) code.

After implementing the code with the SP fix, I launched my mod, no problems in the main menu, but once I start a map, VS2010's debugger tells me that there is a "Unmanaged exception in 0x2268029a (client.dll) in hl2.exe : 0xC0000005: Access violation while reading the position of 0x00000860" and point me to the return line of CBaseCombatWeapon::IsIronsighted( void )

During the debug, if I click on "Automatic" window, VS2010 tell me that m_bIsIronsighted.m_Value = ??? with a CXX0030 error : "Can't evaluate expression".

Any clues on how to fix it ?

--Shepard62700FR 03 Mar 2013

Possible Orange Box Code Bug and Fix

Z33ky, please check this out for me.

The Toggle-Command code (3.5) seems to have a coding error for TOB in it.

You specify: CBasePlayer* pPlayer = C_BasePlayer::GetLocalClient();

This errors outs saying that GetLocalClient does not exist (same with CBasePlayer* pPlayer = UTIL_GetCommandClient() by the way).

I think I may have fixed it by changing that to C_BasePlayer *localplayer = C_BasePlayer::GetLocalPlayer();. The compile does not fail this time and Iron Sights sort of works ingame. However the weapon does not raise up to the players view, the view only gets zoomed in. I can PM you a screen on the steam forums if you need one to see what is happening. --Laughing Man 07 Jan 2010

Check out the latest few revisions for that. --z33ky 14:30, 18 January 2010 (UTC)

Renewed (fixed) Article

Fixed errors Code compiles now, ironsights and console-variables work (except for the bugs mentioned in the tutorial)

Sorry for the trouble I made by giving the variables a proper but sometimes wrong name without testing the actual code --z33ky 09:05, 22 Sep 2008 (PDT)

Wiki talk paes are usually left the same, rather then removing them after a few messages :) --Craziestdan 12:50, 22 Sep 2008 (PDT)
I wanted to clear the page because I renewed the article to a compilable level. So basicly the stuff already talked about is useless. --z33ky 13:13, 22 Sep 2008 (PDT)

not-working ConVars & bugs

Thanks z33ky for correcting the code. It still doesn't work for me though, I'm getting the same problems as Jlea. Except when I have viewmodel_adjust_enabled set to 0, the gun goes half into ironsight position but then immediately goes out again. --Johnmagicson 13:24, 22 Sep 2008 (PDT)

It completly works for me and I have an exact copy of that code. Are you sure the variables are networked correctly? (I just added a little note there) --z33ky 15:56, 22 Sep 2008 (PDT)

Oh, sorry, I assumed it would've gone into the first network table, and that was what caused all my errors and all the problems I had in game. Now it works fine, thanks z33ky!

EDIT: The only thing that doesn't work for me now is using the console commands. In Jorg's version, I could tweak the ironsight co-ordinates to find the right ones to put in my scripts, but in this one the viewmodel_adjust commands do nothing, and turning on viewmodel_adjust_enabled just makes it so I can't aim in ironsights :S --Johnmagicson 22:39, 22 Sep 2008 (PDT)

Did you update the flags to FCVAR_REPLICATED? Else set a breakpoint on the two returns in GetIronsightPositionOffset to see which one is executed, and why. --z33ky 06:02, 23 Sep 2008 (PDT)
so, does it work now? :S --z33ky 08:44, 27 Sep 2008 (PDT)

To which network table Johnmagicson do you put networked vars? I'm getting the same problem and I putted vars to DT_BaseCombatWeapon network table and weapon model is still going out after toggling ironsight. Help me --Dexter127 08:27, 23 Jul 2012 (PDT)

Doesnt work for me

This code doesnt work for me. when activated, when i move or turn my gun goes off in randomd irections and is usually off the screen. I dont get ironsights at all.

SImple. Your not a programmer. Otherwise you'd figure this. --Craziestdan 17:47, 30 Oct 2008 (PDT)
Are you sure you did everything in baseviewmodel_shared.cpp right? Try to use this code without any offsets - does it still happen? --z33ky 09:03, 3 Nov 2008 (PST)

ConVar Placement

Apparently it's not cool, or something, to help out people who are trying to learn through this code(even though we're reverse-engineering someone else's thousands of lines). But I'm still going to ask and hope for a quick, and hopefully helpful response. I have one sole error regarding the placement(I'm assuming) of the ConVars from [1]. Is there a more specific area I should be placing them? "After the includes" doesn't seem to be working. --Vradinolfi 06:50, 12 October 2009 (UTC)

I'm a little confused by your first sentence, but for your ConVar-problems: What exactly is the error?
It should be working just fine somewhere in the global scope.--z33ky 20:03, 16 October 2009 (UTC)
I was just pointing out how pretentious some of the other people here are, don't worry, you're actually helpful. I'm not by my dev box at the moment but it says something like "formerly undeclared identifier" I believe.
It's the only error I get, but I receive it twice and they both point to the ConVars. --Vradinolfi 07:49, 17 October 2009 (UTC)
The only thing I can think about would be that you either didn't put them in global scope, have a typographical error or that you are declaring them after defining the function that uses it.
And make sure it's not between some pre-processor directives like #ifndef CLIENT_DLL or #ifdef HL2MP or something alike. --z33ky 12:44, 17 October 2009 (UTC)

little issue

I have tested this ironsight function, it works correct, but... when i drop weapon with ironsight enabled and then i pick it up i have got a problem with weapon position, the position is same with enabled ironsight, but it should be returned to normal position. So any ideas how to fix this ? Like a "checker" in drop function with disable ironsight function. --Light Kill 06:13, 17 May 2012 (PDT)

old code

not compilable

Z33ky, i followed it and it come up with doezen's of errors :( Probably because my lack of C++ expert :( Could you possibly add line numbers to each section of code? Because some parts im unsure of, so i end up adding in the wrong parts and generate a million errors. Please, add line numbers for all parts :) --Craziestdan 08:46, 14 Sep 2008 (PDT)

Could someone please add line numbers for this tutorial please?? --Craziestdan 17:32, 15 Sep 2008 (PDT)
The reason I won't add lines is, that I don't want newbies to just copy&paste that code, but understand what they are doing. Also, the lines will be wrong if there is a code-update or you already modified one of the files. You should be able to do this with knowledge of C++ and basic Source-knowledge --z33ky
Well, I'm fairly good at coding but I can't figure out why CBasePlayer::SetFov() is missing.
Could it be outdated? EDIT: It's looking for the definition of SetFOV in c_baseplayer.h instead of player.h. Any idea why?
EDITEDIT: Error: 2>..\game_shared\basecombatweapon_shared.cpp(80) : error C2039: 'SetFOV' : is not a member of 'C_BasePlayer' ::::::-Marche
This code is written for OrangeBox-based mods. I'll add this feature to an ep1-mod soon, so maybe you wanna wait or try to figure it out by yourself.
Looking at the code you'll have to paste the SetFOV between #ifdef GAME_DLL #endif and to see if it is able to change the FOV you can look at line 7396 of player.cpp (CBasePlayer::SetFOV). --z33ky 06:49, 6 Nov 2008 (PST)
Well, the crossbow works with a CBasePlayer pointer too, so I have no idea why this doesn't work... It would be great if you could port it for others though! Ill mess with it until you take a look, my mod won't compile, bone_setup.sbr won't regenerate for some reason..... I've switched accounts on the steam forums, I am formerly C++Ftw and now known as Mandelgroove. All forms of Marche were taken and that was a very stupid name ;-;. -Marche
Could you write it your way? I can't get it to work. I still don't see why the crossbow scope works and this doesn't...
FIXED: GAME_DLL isn't defined so it does nothing. Just put #ifndef CLIENT_DLL and #endif around EnableIronsights() and DisableIronsights()

ConVars not working

Hey z33ky, the tutorial works great and I'd like to thank you so much for putting this up. One problem is the console commands do not work ingame, they have no effect.--JLea 21:27, 19 Sep 2008 (PDT)
You need to set viewmodel_adjust_enabled 1 and for that you need sv_cheats to be 1, too. --z33ky 01:28, 20 Sep 2008 (PDT)

EDIT: And you have to make viewmodel_adjust_fov to a ConCommand, changing the fov if viewmodel_adjust_enables is 1.

I'm not that stupid :P. Even if sv_cheats are 1 and viewmodel_Adjust_Enabled is 1 it doesn't do anything. Acutally if adjust_enabled is set to 1, pressing the ironsights button has no effect even if I type new values in. --JLea 02:15, 21 Sep 2008 (PDT)
I'm not sure what's the problem then - I guess you'll have to debug your way through. --z33ky 05:17, 21 Sep 2008 (PDT)

not compilable

The other tutorial was harder to follow, but it worked for me... This one just gave me tons and tons of errors. I have some knowledge in programming in C, and I have made a few mods for hl2 before. I'm pretty sure I followed every step correctly.

The errors tell me there are undeclared identifiers for several variables, and when I look through your code, I can't find any part where you declare some of them, such as "m_bIsIronsighted"... Am I supposed to do some of the coding work myself? I thought this was just an easy copy and paste tutorial :(

EDIT: I also noticed there are some spelling errors, such as m_bIsIronsighted is spelled incorrectly as m_bIsIronighted and "position" is sometimes spelled as "positon"... correcting these spelling errors has reduced the amount of errors for me, i'm still looking through the code. I'm just curious, has anyone tested this and got the same results as the other tutorial?

EDIT: after correcting several spelling errors, i finally got it to compile... but in game when i toggle ironsights, it does nothing!.. I modified some of the code and I managed to get it to go in and out of ironsight aiming, but it can never stay! It just makes the gun go into ironsight position for a second and then immediately goes out. --Johnmagicson 22:09, 21 Sep 2008 (PDT)

Undeclared identifiers?

I can't seem to get this work. The compiler just whines that all of the variables and functions are undeclared, even though they are declared, I've checked all of them, and tried them in a few different places. Any idea why this could be happening? I've also made sure they're not misspelt. --Dragory 12:43, 14 January 2010 (UTC)

Which variables does the compiler deem as undeclared? --z33ky 14:30, 18 January 2010 (UTC)
Every variable added in this tutorial --Dragory 10:16, 13 March 2010 (UTC)
Upload some header (let's say basecombatweapon_shaded.h) to a pastebin and post the link, so I can take a look at it. --z33ky 13:45, 14 March 2010 (UTC)


Where is weapon_smg1.txt located, or is it supposed to be .cpp or .h? --Adam.gamedev 16:47, 25 May 2010 (UTC)

It's in the mod\scripts forlder and it is a .txt file. MLSTRM 10:27, 2 May 2010 (UTC)

BTW, ign ur posts using 4x"~"

One Compiling Error

I get this error when trying to compile,

1>c:\Program Files\Steam\steamapps\[email protected]\sourcesdk_content\TFDMod\src\game\shared\basecombatweapon_shared.h(322) : error C2061: syntax error : identifier 'CSendProxyRecipients'

Something tells me that this should be easy to fix, but I don't know what's happening. what's wrong with the identifier, or what's the syntax error? :( --Stoopdapoop 22:28, 1 June 2010 (UTC)

Without seeing the code I could only guess that you might have added the variables outside the class. If that is not the case, post your code on a pastebin and link them here. --z33ky 14:40, 2 June 2010 (UTC)

weapon spread

You know, you're not actually limited to the pre-calculated degrees. You can divide or multiply them to get narrower cones. Dividing by 60 gives (degree) MOA, so you can get 3MOA by dividing the 3deg define by sixty. Keep in mind that this isn't as useful as it sounds; HL2 combat takes place at what are considered *very* short distances in real life - almost always less than 1500ish units (30 meters) and usually less than five meters. If you're going to bother going this route (incrementing accuracy by 1MOA steps or even lower), you might as well implement a more complex ballistic simulation, including bullet penetration and wounding effects. Otherwise, it just makes combat nearly one-burst (or even one-hit!) kills, which is incredibly lethal and won't really be playable for most FPS games (you ArmA2 people can pipe down now, I know you're masochistic enough to *like* ultra-realistic lethality settings, but that's not actually what a lot of mods want). --Silverpower 12:56, 31 December 2010 (UTC)


Getting errors from this. Any hints on where it goes?--Welsh Mullet 21:53, 15 February 2011 (UTC) Problem is with the line:

void RecvProxy_ToggleSights( const CRecvProxyData* pData, void* pStruct, void* pOut );
Maybe you need to add #ifdef CLIENT_DLL before this finction, and #endif after it? --Wolf480pl 04:27, 13 May 2012 (PDT)


OMG this method is wrong! Someone swapped Enable and Disable! --Wolf480pl 09:56, 15 May 2012 (PDT)

Whoops! Fixed. --z33ky 01:28, 21 July 2012 (PDT)

Does this work in MP now?

As on Jorgs Code said, this code should only work in SP, but is there a way to get it into MP (Bullet Spread etc.)?

The page is wrong. This code was written up specifically to work well with MP mods. --z33ky 01:28, 21 July 2012 (PDT)

I tried to set this up, but I noticed some of the things shown in here don't seem to work for SP Orangebox engine. I keep ending up with build errors like that it can't find the "GetLocalPlayer" identifier or saying that "ServerCMD" is not a member of "IVEngineServer". All of the errors are coming from basecombatweapon_shared.

Ironsight Bug

I binded my "toggle_ironsight" to mouse3 but whenever I use Ironsight the V_model for whatever gun I am using flips and ends up being upside down, I am making a mod and do have C coding experience but I cant seem to fix this Problem. I know that the offsets are fine because i've used the same offsets before but was wondering if a Third-person modification would cause this. Any help would be apperciated.

Is it possable to edit player movement speed while ironsighted?

So, is it? 'Cause I think ironsighting with no downside seems over-powered. So could someone tell me how to impelemt it or where to look at to impelemt it? -TwoTails (1/12/2015)

Ironsights while sprint won't go away.

I tried to do a lot of things to do that, but unfortunately my programming skills are pretty bad, and I can't fix this thing. So, when you start sprint Ironsights won't go away and stay on screen before I press my toggle_ironsight binded mouse2. --FunGamePlays(30/07/19)

I didn't understand anything at all

I didn't understand anything at all. Could you explain to the teapot in programming how this is done? SlasteliNVL (06.06.2020)