1

I'm trying to pause one section of code. I've tried doing a for loop which isn't very effective and I only want to pause one section.

Would wait() work or does that pause the whole program?

I'm making a game for my course and the only way I can think of to make AI to move at a set speed is to pause them and move every 1-3 seconds (for example).

Does anyone have a solution for me?

Edit: I was apparently being an idiot. Problem solved without any wait() functions. Thanks for all the help!

  • 1
    If that 'section' of code is executed by a particular thread, usleep() for unix (and probably Sleep() for windows) pauses only that thread, the others continue. – 2785528 Jan 03 '16 at 04:06
  • 1
    Is this windows or console program? What do you mean by "section of code"? What `wait()` function are you referring too? – Barmak Shemirani Jan 03 '16 at 06:26

1 Answers1

1

It depends of how are you running the AI. If the AI has a thread that you can pause, that may work. But I personally would go for another approach:

  • Try to make the velocity relative to the delta time of the previous frame.

    Movement+=velocity*dt;  //velocity per second * time of previous frame in seconds
    
  • Have a fixed framerate and use the magic number (your fixed delta time) to achieve the same effect than before.

(sleep Sleep for milliseconds)

  • Use a counter and an if to run that section of your code only in certain moments.

    ...
    time+=dt;
    if (time>3){
    ...
    time=0.0f;
    }
    
Community
  • 1
  • 1