Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » Run function after X miliseconds
Run function after X miliseconds [message #417956] Mon, 18 January 2010 10:35 Go to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
I currently have a piece of code that will run Function A on level loaded

This function initiates Function B a little later.
In Function B im getting the length of an MP3 file (in miliseconds)
After this, Function B has to run again after that time, while any other code still runs.

Problem is i cant use Sleep(); or w/e to wait that long because all other code must continue running.
Afaik i cant make it a script either (so i can use Start_Timer + Timer_Expired) that will be attached to a player because it'll will then restart the whole code after every die (AKA object change)

void FunctionA()
{
	//Code here; read file containing sounds
	

	FunctionB();
}

void FunctionB()
{
	if(runnow)
	{
		//Pick a random song from the file
		//Play the song

		fsystem->update();
		{
			//Set Volume

			//Get song length (in miliseconds)
		}
		FunctionB(); //This should run after 'song length' above
	}
//More code; should still run and not only after X miliseconds as Sleep(); would do
}


If needed i'll upload the whole file

EDIT:
i believe in Javascript you have something like setInterval() that does what i want Razz

EDIT2:
Oh and if anyone knows how to get the music volume settings from renegade, please tell ^^


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Mon, 18 January 2010 10:42]

Report message to a moderator

Re: Run function after X miliseconds [message #417961 is a reply to message #417956] Mon, 18 January 2010 10:43 Go to previous messageGo to next message
Sir Kane
Messages: 1701
Registered: March 2003
Location: Angerville
Karma: 0
General (1 Star)
DWORD CALLBACK ThreadProc(void* pArg){
	DWORD delay;
	delay = PtrToUlong(pArg);
	Sleep(delay);
	//Do your shit here
}

HANDLE hThread;
DWORD tid;
hThread = CreateThread(NULL, 0, ThreadProc, ULongToPtr(delay), 0, &tid);
CloseHandle(hThread);


Might want to make it thread save, though.


Proud N9500 and proud N6270 user. Creator of the IEE libraries (original bhs.dll) and the RB series software.
http://n00bstories.com/image.fetch.php?id=1189992501http://www.n00bstories.com/image.fetch.php?id=1257492907
Re: Run function after X miliseconds [message #417966 is a reply to message #417956] Mon, 18 January 2010 12:05 Go to previous messageGo to next message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
If i understand you correctly (i think this is multi threading which is new for me) it should be something like this??

HANDLE hThread;

DWORD CALLBACK ThreadProc(void *pArg)
{
    while(playnow)
    {
        DWORD delay;
        delay = PtrToUlong(pArg);
        Sleep(delay);
        FunctionB();
    }
    return 0;
}

void FunctionA()
{
	//Code here; read file containing sounds
	

	FunctionB();
}

void FunctionB()
{
	if(runnow)
	{
		//Pick a random song from the file
		//Play the song

		fsystem->update();
		{
			//Set Volume

			//Get song length (in miliseconds)
		}
                DWORD tid;
                hThread = CreateThread(NULL, 0, ThreadProc, ULongToPtr(songlength), 0, &tid);

	}
//More code; should still run and not only after X miliseconds as Sleep(); would do
}

void FunctionC()
{
     //Close file etc
     CloseHandle(hThread);
}



http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Mon, 18 January 2010 12:27]

Report message to a moderator

Re: Run function after X miliseconds [message #417969 is a reply to message #417956] Mon, 18 January 2010 12:32 Go to previous message
Omar007 is currently offline  Omar007
Messages: 1711
Registered: December 2007
Location: Amsterdam
Karma: 0
General (1 Star)
OK great it works Very Happy

I only got 2 more things;
1. How can i retrieve the music volume settings of renegade?
2. When i quit the game in progress it doesnt stop playing. Atm i got the Stop(); function called on GameOver Hook but quiting the game wont be hooked by this function. Any solution or other hook?

EDIT:
Ok i was thinking why it wouldnt be easier to multithread with a class or w/e and i found this online:
http://www.codeproject.com/KB/threads/SynchronizedThreadNoMfc.aspx

It works really easy ^^
So far all lazy ppl like me (Razz) get it Wink


http://tiberiumredux.omarpakker.nl/Old Unused Parts/Plaatjes/PromoteBanner_Hades_small.jpg

[Updated on: Tue, 19 January 2010 07:58]

Report message to a moderator

Previous Topic: Hud Help
Next Topic: Islands gmax files?
Goto Forum:
  


Current Time: Wed Dec 18 21:42:10 MST 2024

Total time taken to generate the page: 0.00854 seconds