Calling Functions within Registered Global Functions

Started by
5 comments, last by WitchLord 19 years, 3 months ago
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?
Jayanth.KRaptor Entertainment Pvt. Ltd.http://www.raptorentertainment.com---------------------------------------------------------Why Mr. Anderson? Why? ...Why keep fighting? Do you think you're fighting for something - for more than your survival? Can you tell me what it is? Do you even know? Is it freedom, or truth, perhaps peace, could it be for love? Illusions Mr. Anderson, vagaries of perception. Temporary constructs of a feeble human intellect trying desperately to justify an existence without meaning or purpose.
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
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
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();}

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

is there a way i could keep results from GetFunctionIDByDecl or GetFunctionIDByName somewhere or do i need to replace it often?
Jayanth.KRaptor Entertainment Pvt. Ltd.http://www.raptorentertainment.com---------------------------------------------------------Why Mr. Anderson? Why? ...Why keep fighting? Do you think you're fighting for something - for more than your survival? Can you tell me what it is? Do you even know? Is it freedom, or truth, perhaps peace, could it be for love? Illusions Mr. Anderson, vagaries of perception. Temporary constructs of a feeble human intellect trying desperately to justify an existence without meaning or purpose.
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.

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

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?
Jayanth.KRaptor Entertainment Pvt. Ltd.http://www.raptorentertainment.com---------------------------------------------------------Why Mr. Anderson? Why? ...Why keep fighting? Do you think you're fighting for something - for more than your survival? Can you tell me what it is? Do you even know? Is it freedom, or truth, perhaps peace, could it be for love? Illusions Mr. Anderson, vagaries of perception. Temporary constructs of a feeble human intellect trying desperately to justify an existence without meaning or purpose.
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.





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

This topic is closed to new replies.

Advertisement