Call Function Every X Seconds?

Hello people,

This should be simple enough but I can't seem to figure it out. Been up all night working with DirectX but thought I would post this here for some help. All I want to do is call a function every ten seconds. What would be the best way to go about this. Have tried it with all the basic methods such as for, while and do while. Here is the for loop to show you what I mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
void HelicopterMovement()
{
	
	for (int i = 0; i<=10; i++)
	{
		cout << i << endl;   // just used to display output.
		if (i == 0)
		  {
     helicopter->Start(); // this plays a helicopter sound in a 3d soundscape.
		  }
        Sleep(1000);
	}
}


This only plays the helicopter->start once as far as I am aware. The display outputs 1-10 in a loop as expected. Any help on this one would be appreciated.
hmm..
why don't you try this
1
2
3
4
5
6
7
8
void HelicopterMovement()
{
	while (true){
              helicopter->Start();
              Sleep(10000);
        }

}


edit: added code tag
Last edited on
Thanks for the reply; but helicopter sound does not play at all when I do that and I get the loading circle over the window. However I did get the sound playing a repeated number of times with.

1
2
3
4
5
6
7
8
9
10
11
void HelicopterMovement()
{
	for (int i = 0; i<=10; i++)
	{
		        cout << i << endl;  // just used to display output.
				
			helicopter->Start();  // helicopter in 3d soundscape.
			Sleep(1000);
			helicopter->SetPosition (-10.0, 0.0, 0.0);
	}
}


So I reset the position every 10 seconds and the helicopter will move from that spot again. The program window seems to be less responsive now; any advice on how to improve this?
First things first,
you are playing the sound every 1 second (1000 ms = 1 sec).
And you do know what Sleep() does don't you? It freezes your program. Which means:
your program calls helicopter->start()
it freezes for 1 second (And by freezing, I mean it does nothing at all)

and the cycle continues for 10 times. and then execution gets out of the loop.
Where is HelicopterMovement() being called from?

You mention DirectX, so this is some kind of Windows GUI app. Are you using a normal message loop, or some sort of game loop?
Thank you Pravesh that cleared things up for me and the program is sounding much better now.

Hi Andy the HelicopterMovement() is being called from the main .cpp file for now as it calls from the HelicopterSound class.

Thanks for the replies but I think the effect has been accomplished with Direct Sound without the need for a repeat function every x seconds.
Topic archived. No new replies allowed.