-
Skipping functions
Ok... well I've been trying to skip a function ONCE just to give a breif second inbetween to accomplish something before the other function is run so it doesn't run at the same time. I was thinking of using the goto function but that is bad programming practice from what I hear :).
So, what else could I do?
-
Unless your running a thredded program or using a program designed to run in parallel on a multiprocessor system I don't think you need to worry. Post some code and describe what you are trying to do with it.
... yes, goto is generally frowned upon here and should be avoided if at all possible.
-
if you want to break out of the function while your in it use return;
-
Elasto you have just opened my mind. Thanks, will post back on success.
-
The return statement is used to quit any function you're in and return to the calling function. Also, goto doesn't skip between functions.
-
Ok, I thought you guys had helped me solved this but the way I used the return command didn't work afterall so I will revive this topic to further abstruct the solution. I am in school right now and will post the code when I get home, but for now I will explain the two main functions I'm using.
I am working on a modification for the computer game Half-Life 2. I am under a weapons file called weapon_357.cpp and am trying to make it so if the user clicks to fire, the first shot doesn't fire but instead if aims the weapon, thus allowing to fire after.
PrimaryAttack() and CheckZoomToggle() are my two main functions that should be the only ones I need to worry about. PrimaryAttack() tells to fire everytime the person clicks and I made the CheckZoomToggle() function to say to zoom if they press the fire button, unzoom if they right-click and so forth.
I am thinking these two functions are colliding with eachother when they player tries to fire, the CheckZoomToggle() function brings up the weapons zoom. I tried making a few if statements to say like...
Code:
if(!pPlayer->SetFOV( 0, 75f, 0.2f )
{
return;
}
in the PrimaryAttack() function.
I have tried many other possible ideas with this, but I only have one idea left which is creating a global variable and incrementing it in the PrimaryAttack() function, also I will make an if statement with something like...
Code:
if( shotCount < 2 )
{
return;
}
I have yet to test this and will when I get home. I'm doubting it will work so I would like ANY ideas out there. Theres another function which checks every frame for activity if they may be of use. This function is called PostItemFrame().
Thank you ahead of time.
-
The Source engine is huge and complicated. You probably need to toy about with "Think" functions to get this to work, and set the next 'think time' for your weapon (offset from gpGlobals->curtime - which is the current game time)
This question is probably better directed at VERC Collective forums http://collective.valve-erc.com or Wavelength forums http://www.thewavelength.net
edit: here's some mock code - it's been a long time since I did anything with the Source engine. You really ought to ask on VERC for a better answer.
Code:
DEFINE_THINKFUNC(ShootWeaponThink);
DEFINE_THINKFUNC(ZoomSightThink);
DEFINE_THINKFUNC(IdleThink);
CMyWeapon::PrimaryAttack()
{
// Maybe some code here
pPlayer->SetFOV(0, 75f, 0.2f);
SetThink(&CMyWeapon::ShootWeaponThink);
SetNextThink( gpGlobals->curtime + 0.5 );
}
CMyWeapon::ShootWeaponThink()
{
// weapon firing code
SetThink(&CMyWeapon::IdleThink);
SetNextThink( gpGlobals->curtime + 0.05 );
}
-
Instead of using a global variable, you can use a local static variable instead:
Code:
void f( ) {
static int countDown = 3;
if ( countDown-- ) /* The first three times this function is called. . . */
return; /* It just returns. */
/* But after calling the function three times we get here, etc. */
}
P.S. This idéa of skipping execution of a function for the reason you mentioned sounds like a bad idéa. In my oppinion You should seek a completely different solution.