When to prepare context?

Started by
3 comments, last by acjh13 5 years ago

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.

Advertisement

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.

 

 

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

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.

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

This topic is closed to new replies.

Advertisement