• Advertisement
Sign in to follow this  

ExecuteString in Object?

This topic is 2329 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 it possible to call a method in an object by passing a string, like is done with ExecuteString in scripthelper.cpp?

I check the code and from what I can see it is not possible to extend to include object methods as that requires knowledge of the actual object pointer (which CompileFunction does not).


So is this possible in some other way? And if so, how do I go about?


Reason for wanting this, is because it is a more secure way for script to call methods in other modules.

Share this post


Link to post
Share on other sites
Advertisement
While CompileFunction doesn't allow you to compile a class method you could quite easily write an automatic wrapper that takes the object pointer as one of the parameters, and then call the method. For example:



int CallObjectMethod(asIScriptObject *obj, const string &method)
{
string wrap = "void WrapCall(" + obj->GetObjectType()->GetName() + "@ obj) { obj." + method + "(); }";

asIScriptFunction *func = 0;
int r = execMod->CompileFunction("ExecuteString", wrap.c_str(), -1, 0, &func);
if( r < 0 )
return r;

asIScriptContext *execCtx = engine->CreateContext();
r = execCtx->Prepare(func->GetId());

if( r < 0 )
{
func->Release();

if( !ctx ) execCtx->Release();

return r;
}

execCtx->SetArgObject(0, obj);

// Execute the function
r = execCtx->Execute();

// Clean up
func->Release();
execCtx->Release();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement