waiting instead of for()

Started by
7 comments, last by Rain Dog 18 years, 7 months ago
My app will be single-threaded, and I would like all script logic to be done within the same game frame, so instead of: for(;;) { ... if( quitTimeOut && quitTimeOut < timeGetTime() ) ... } isn't there a function that will wait until: a) the script finishes executing its method b) a timeout defined in my C++ occurs whichever comes first? It seems that rolling around in a loop would eat processor cycles that the script could be using. I'm using a MingW compiler.
Advertisement
How about sleep? You pass it the number of milliseconds to sleep:
// This will waste yet another second.sleep( 1000 );


Greetz,

Illco
if the script finishes early, how do I "wake up" my C++ code asap?
Since your application is single threaded, it is either executing a script or it is processing your application code. The application is already waiting for the script to finish, so you don't need a function for that. The Execute() method only returns when the script finishes or is suspended by a call to Suspend(), either from the line callback or from an application function called from the script.

If your scripts finishes early and you don't want to start the next frame yet, then you could use sleep() to have the application wait until it is time to start the next frame. However, why would you want to do that?



AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Let me rephrase the question:

In my single-threaded app, I want the script to run for 3 milliseconds at most. If the script finishes early, I need it to return control to the C++ program right away. I can only trust the C++ program to enforce these 2 rules.

How can I do this?
On an entirely different note, while (true) is generally clearer than for (;;).
fuzinavl:

I show how to implement timeouts for scripts in the sample Events. You should look specifically on how the line callback function is implemented.

Deyja:

while(true) may be clearer (it's a matter of opinion) but it is not accepted without warning by all compilers. The same compilers don't complain about for(;;) though, so my recommendation is to go with for(;;).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

r = mainCtx->SetLineCallback(asFUNCTION(LineCallback), &timeOut, asCALL_CDECL);

doh! (again). I was interchanging timeOut and quitTimeOut variables.

I didn't realize AS had this situation taken care of for me *and*, I can define what should happen in the situation! YAY!

Hopefully I can wrap this all up in a simple .h library and share it around soon.
Steve McConnell recommends making a define.

#define LOOP_FOREVER for(;;)

for better clarity.

This topic is closed to new replies.

Advertisement