Skipping functions

This is a discussion on Skipping functions within the C++ Programming forums, part of the General Programming Boards category; Ok... well I've been trying to skip a function ONCE just to give a breif second inbetween to accomplish something ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    if you want to break out of the function while your in it use return;
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    Elasto you have just opened my mind. Thanks, will post back on success.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    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.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    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 ); 
    }
    Last edited by Bench82; 04-20-2006 at 01:18 PM.

  8. #8
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    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.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21