Jump to content
  • Advertisement
Sign in to follow this  
GameEngineer_gi

Scripts and the game loop

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to understand how you would run a script (AngelScript) along side of a game loop. I may be reading too much but recently was reading in "Game Scripting Mastery" where you may want to have your script execute one line at a time (command-based scripting) for each frame. A frame would be a single pass through the game loop, obviously. I guess I'm picturing AS to be more of a "one-shot" kind of deal. When you execute the script, the program pointer blocks in C++ at that location until the script has been fully run. Can someone help me understand this? Oh, and just thought of another question. Can one script "run" or call another script from script code itself? I've definitely seen this done in Lua. Thanks. Steve

Share this post


Link to post
Share on other sites
Advertisement
Anyone? Beuler? :)

In my own continued research I can see that you can suspend and re-execute the script.

It's not apparent in the documentation where the execution starts up after it has been suspended. Does it start at the last place it left off or does it reset and start at the beginning of the script?

I will do some more experimentation.

-Steve

Share this post


Link to post
Share on other sites
Sorry for not responding earlier. I've been extremely busy. For the same reason my answer will be short.

You'll use a line callback function to suspend the script execution after each line, and then resume the execution the next frame by calling Execute() again. The script will resume where it left of, it doesn't restart from the beginning.

A script can call another script if your application registers a function to do this. The application function would then create a new context and execute the new script in that one.

Take a look at the sample applications in the /samples/ directory of the SDK. It will show you how to do all this.

Regards,
Andreas

Share this post


Link to post
Share on other sites
Thanks Andreas!

Using LineCallBack callback function to do something like pContext->Suspend() and then somewhere in my game loop call pContext->Execute()? Ok, let's see if I did this right.

//----------------------
// Register the callback
//----------------------
pContext->SetLineCallback(asFUNCTION(MyPerFrameCallback), NULL, asCALL_CDECL);

//-----------------------------
// The script callback function
//-----------------------------
void MyPerFrameCallback(asIScriptContext *ctx /*, void* */)
{
// Suspend the script
// One line from the script has been processed.
ctx->Suspend();

}

...

//----------
// Game loop
//----------
void MainLoop()
{
// You know! :)
ProcessInput();

// Resume the script processing
pContext->Execute();

// Update the frame
FrameUpdate();

// Render
RenderFrame();
}

....

-Steve

[Edited by - smjones on January 31, 2007 2:07:47 PM]

Share this post


Link to post
Share on other sites
That's correct.

You might want to check the return code from ctx->Execute() though. It should be asEXECUTION_SUSPENDED, otherwise the script has stopped for one or another reason.

Share this post


Link to post
Share on other sites
Just wanted to give a quick update. My little test app using the above code works great! Each iteration of the game loop executes 1 line of script code. In effect, that particular script is running synchronously with the game loop.

-Steve

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!