Running a function for a period of seconds

This is a discussion on Running a function for a period of seconds within the C++ Programming forums, part of the General Programming Boards category; Well, I wanna run a function for a fixed period of seconds, like 5 seconds or so, how do I ...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    Running a function for a period of seconds

    Well, I wanna run a function for a fixed period of seconds, like 5 seconds or so, how do I go about doing this?
    I've looked through ctime reference, and I dunno which of those functions to use for this, or how :S
    Any advice?
    Thanks in advance!
    Currently research OpenGL

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Does a single run of the function take more than 5 seconds?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    5 seconds was just some number that got in my head, I'm trying to make the character jump, and I need it to go upwards for a short period of time
    Currently research OpenGL

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Ah, so not really a specific function then.

    Just part of a normal game loop which
    do
    - detects if there is any key press
    - performs actions on the key press
    - moves NPC characters
    - redraws the screen
    - works out alive-or-dead
    while !dead

    Except now it would be
    do
    - detects if there is any key press
    - performs actions on the key press
    - perform any player auto movements (such as jump)
    - moves NPC characters
    - redraws the screen
    - works out alive-or-dead
    while !dead

    At the start of a jump, you determine a trajectory (didn't you ask about gravity earlier?). The player character continues along this trajectory (one screen refresh at a time) until something interesting happens (say landing on another floor, grabbing a rope, dying horribly etc).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, I kinda got it working, but not well enough xP
    Now the player just spawns up in the air and falls down D:

    Code:
    void character::goUp(float dt){
    
    	float tempY = y;
    
    	do{
    		y -= gravityPull + 100 * dt;
    
    	}while(tempY <= y + 200);
    }
    how can I avoid this? xP
    Currently research OpenGL

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Akkernight View Post
    ok, I kinda got it working, but not well enough xP
    Now the player just spawns up in the air and falls down D:

    Code:
    void character::goUp(float dt){
    
    	float tempY = y;
    
    	do{
    		y -= gravityPull + 100 * dt;
    
    	}while(tempY <= y + 200);
    }
    how can I avoid this? xP
    Well, look at what it does -- it moves y down by 300 units, I guess. If you want to draw the person going up (I believe in your system y going down is player going up?) then you can't do it all in one function, since the drawCharacter doesn't get a chance to act until this function returns.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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