🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Exposing operator function pointer

Started by
2 comments, last by WitchLord 9 years, 6 months ago

I was wondering how I would go about registering a function pointer operator like

Object& operator() (void (&func)()) ;

at the moment I have


MYASSERT(e->RegisterObjectMethod(CN, CN+" &opCall()(void (&func)())", asMETHODPR(Object, operator(), (void(&func)()), Object&), asCALL_THISCALL) >= 0);

I am getting these errors


/*******************************************************/
System function (1, 24)
ERROR:  : 
Expected '<end of file>'
Has Compile Errors: True
/*******************************************************/


/*******************************************************/
System function (1, 24)
ERROR:  : 
Instead found '('
Has Compile Errors: True
/*******************************************************/


/*******************************************************/
 (0, 0)
ERROR:  : 
Failed in call to function 'RegisterObjectMethod' with 'Object' and 'Object &opCall()(void (&func)())' (Code: -10)
Has Compile Errors: True
/*******************************************************/
Advertisement

Are you certain you want to expose this function to the script? A C++ function pointer is not compatible with the script function pointers. How do you plan for the script to obtain the C++ function pointer to pass to the operator() method in the first place?

Anyway, if you really do want to expose it, I suppose you could do something like this:

// First register a type to represent the C++ function pointer
engine->RegisterObjectType("cfunc", sizeof(void (*)()), asOBJ_VALUE | asOBJ_POD | asOBJ_PRIMITIVE); 
 
// Register the operator() as an opCall method receiving the cfunc type by value
engine->RegisterObjectMethod("MyObj", "MyObj &opCall(cfunc)", asMETHOD(Object, operator(), (void(&func)()), Object&), asCALL_THISCALL);

In the above I'm registering the cfunc as a POD type, but if you really want to expose this method to the script you should probably register it as a non-POD and provide appropriate constructors and assignment operators to guarantee that the function pointer is always valid. Otherwise the script might call the opCall() method with an invalid function pointer and your application will crash (or it might be exploited by malicious users to hack the computer).

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

The object I am exposing takes a function and is a value|pod type, what I wanted to do is pass an angelscript function to the C++ object not the other way around. then the question would that work? I guess I could always just make a proxy between these, send a function in C++ to the object which calls a function in angelscript.

in angelscript what I planned was it would look like


void func(){
  //do operations
}

podobject(func);

and on the C++ side it would just call it as a angelscript function.

It's not REALLY needed but I figured it would be easier than doing


void Function(){
  context calls
  function= "angelscript function here"
  execute
}

in C++ then declare another function in angelscript for the script function call.

Then you want to do the following:

engine->RegisterFuncdef("void CALLBACK()");
engine->RegisterObjectMethod("MyObj", "void opCall(CALLBACK@)", asMETHOD(Object, operator(), (asIScriptFunction*), void), asCALL_THISCALL);

Take a look at this article in the manual for more details.

Regards,

Andreas

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