Jump to content
  • Advertisement
Sign in to follow this  

Calling Functions within Registered Global Functions

This topic is 4876 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

is there a way i can call the script functions via a function registered via RegisterGlobalFunction /// script /// void testscrfunc() { GetCurrentVal(0); } void calledfunc() { // do nothing } //// code ////// void GetCurrentVal(int i) { ExecuteScriptFunction(engine, context, "calledfunc" ); } ////////////////// is this possible? or is there any workaround?

Share this post


Link to post
Share on other sites
Advertisement

void GetCurrentVal(int i)
{
asIScriptContext *ctx;
engine->CreateContext(&ctx);
ctx->Prepare(engine->GetFunctionIDByDecl("module", "void calledfunc()"));

int r = ctx->Execute();
// If r==0, it worked
}



If you didn;t specify a module name when you compiled that script section (ie, you just passed 0 or NULL) pass NULL as the first parameter of GetFunctionIDByDecl(..).

Alan

Share this post


Link to post
Share on other sites
Exactly.

I recommend determining the function ID one time only though, and not for every function call (if possible). GetFunctionIDByDecl() is quite expensive as the declaration has to be parsed, and then compared with the compiled functions to find the correct match.

It would also be possible to get the engine pointer and module name from the context.


void CallFunction()
{
asIScriptContext *ctx = asGetActiveContext();
asIScriptEngine *engine = ctx->GetEngine();
string mod = engine->GetModuleNameFromIndex(ctx->GetCurrentFunction()>>16);

asIScriptContext *ctx2;
engine->CreateScriptContext(&ctx2);
ctx2->Prepare(engine->GetFunctionIDByDecl(mod.c_str(), "void calledfunc()");

int r = ctx2->Execute();
if( r != asEXECUTION_FINISHED )
ctx->SetException("Execution failed");

ctx2->Release();
}

Share this post


Link to post
Share on other sites
is there a way i could keep results from GetFunctionIDByDecl or GetFunctionIDByName somewhere or do i need to replace it often?

Share this post


Link to post
Share on other sites
These values only change when you recompile the scripts in which the functions reside. Exactly how you store the values is up to you, it could be in global variables, or perhaps some script manager class.

I recommend that however you store the values, you update them just after compiling the scripts. This would be much like how you would manually load a dll and store the function pointers to the functions you wish to use.

Share this post


Link to post
Share on other sites
in what datatype would it be ideal to sture them?

here:
int funcID = engine->GetFunctionIDByName(0, (const char *)funcname);
int r = ctx->Prepare(funcID);
should i backup the ctx(context) here?

Share this post


Link to post
Share on other sites
No, you don't want to keep too many contexts around, because they consume quite a lot of memory (depending on the stack size). You just need to keep the function ID.

If you have some particular function that is executed very frequently you may experiment with having a dedicated context just for that function. If you call asIScriptContext::Prepare(-1) the context will restore the state using the same function ID as the last time, saving a little time.

But in a game I don't think you'll find much use for this feature. I can't think of a script function that is executed that frequently.

You do not want to have to create a context for each script function call though, instead keep the context around and just call Prepare() on it again when executing another function. If you have the need to call more than one script function at the same time (e.g. a script function calling an application function that calls a script function), you could benefit from a pool of contexts.





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!