Scripts and the game loop

Started by
4 comments, last by GameEngineer_gi 17 years, 2 months ago
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
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
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

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

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]
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.

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

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

This topic is closed to new replies.

Advertisement