Jump to content
  • Advertisement

When to prepare context?

Recommended Posts

Hi all, I'm a new user to AngelScript and have been trying to integrate it into my C++ component-based game engine for the past week or so.
I've finally settled on a design where my script manager will add all the scripts to the script builder, build the module and prepare the context all at the start of loading a scene.Once that is done, the script system will execute the context (but only while the game is playing, if it is paused it will suspend the context).

However, I've been getting an error code asCONTEXT_NOT_PREPARED.

So was I wrong to prepare the context too early? and is there a better design I could follow for a component-based engine?


Thanks for your time and apologies if its too vague or too simple.

Share this post

Link to post
Share on other sites

Are you calling Execute on the same instance of asIScriptContext that you called Prepare on? If Prepare was successful (check the return code) then Execute shouldn't return asCONTEXT_NOT_PREPARED (unless you're calling it on a different instance that hasn't been prepared).


I cannot comment on your design decision. The description is way to vague for that. There is nothing wrong from AngelScript's point of view to prepare the context in one step of the setup to be used at a later step during execution, but the prepare could just as well be done just before the call to Execute in the same execution step. Which is better depends more on your surrounding engine design than AngelScript itself, e.g. how you manage the contexts, how you treat compile time errors versus runtime errors, etc.



Share this post

Link to post
Share on other sites

I suppose you have a "script" component. Lets call it the "Controller" component. When you add a "Controller" component to one of your GameObject, you should "cache" the function pointer (asIScriptFunction) (not the context). Check the "game" sample that comes with Angelscript:

// scriptmgr.cpp Line 217
// Save/Cache this object in your Controller component
asIScriptObject *CScriptMgr::CreateController(const string &script, CGameObj *gameObj)

// scriptmgr.cpp Line 208
// Save/Cache the method you want to call
ctrl->onThinkMethod = type->GetMethodByDecl("void OnThink()");

// scriptmgr.cpp Line 282
int CScriptMgr::ExecuteCall(asIScriptContext *ctx)

// scriptmgr.cpp Line 302
asIScriptContext *CScriptMgr::PrepareContextFromPool(asIScriptFunction *func)

// scriptmgr.cpp Line 318
void CScriptMgr::ReturnContextToPool(asIScriptContext *ctx)

I've been doing this for years now. Never had to touch that code again.

Share this post

Link to post
Share on other sites

Oh dang, I completely forgot to reply here, sorry!

Thanks for the advice guys. Unfortunately, had to stop development on the scripting side of my game engine for the time being due to personal stuff but I will definitely take what you've said when I resume it

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!