Advertisement

funcdefs to member functions

Started by December 31, 2010 08:56 AM
3 comments, last by _orm_ 13 years, 10 months ago
Hi there,
I am currently trying to build an signal-slot system for angelscript. It works fine, with one exception: I can't bind slots of objects to it.

How can I save an function-pointer to an method of one specific object?
testSignal.connect(testObject.printSomething);

Shows the error "'printSomething' is not a member of 'Test'".

How do I take an function as an parameter of an C++ function? I couldn't find anything to that in the documentation. I want to rewrite them as C++ class so I can use them to connect the script with the code. (for example, I have an sprite class which should send an signal as soon as the current animation has ended, so that the script can react on it)

My angelscript implementation: http://pastebin.com/KW8FCLxv
You can't (as far as I know) create a function pointer which points to methods of a specific object, but you can create function pointers which point to methods of a class of objects as follows:

typedef RETURN_TYPE (CLASS_TYPE::* FUNCTION_PTR) (...);

The above creates a type definition of a function pointer (usually the best way to go when defining such pointers) called FUNCTION_PTR, which points to methods of a class called CLASS_TYPE, that returns types of RETURN_TYPE.

You can of course change these names accordingly to suit your needs and place whatever arguments your method takes in the (...).

Using this approach, you can simply define instances of this function pointer type as follows:

FUNCTION_PTR MyNewFuncPtr = &CLASS_TYPE::METHOD_NAME;

Where again, you would simply replace METHOD_NAME with you own choice.

I'm not sure why one needs the "addressof" operator (&), but it seems to be required for these kinds of function pointers.

Hope this helps!
Advertisement
function pointers in AngelScript only supports global functions, so you can't store a pointer to a class method this way.

In the future I will probably implement support for delegates, which would support class methods by keeping a reference to both the object and the method itself.

To pass a function pointer to a registered application function, the application must register the type of function it expects with a RegisterFuncDef call. The function that takes the function as parameter should be implemented to receive a pointer to an asIScriptFunction.


void TakeFunction(asIScriptFunction *func)
{
// Do something with the function

// Release it when done
if( func )
func->Release();
}

engine->RegisterFuncDef("void AppCallback()");
engine->RegisterGlobalFunction("void TakeFuncion(AppCallback @)", asFUNCTION(TakeFunction), asCALL_CDECL);


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

Kay,
Thanks for the answer.

I've now changed connect to taking an object + a string containing the name of the function to connect to.

Simple example:
class Controller : Object
{
Controller()
{
SIGNAL_KeyPressed.connect(this, "handleKeyPress");
}
void handleKeyPress(KeyCode key)
{
if(key == Space)
maChest.activation();
}
};


Thats quite acceptable to me.

Kay,
Thanks for the answer.

I've now changed connect to taking an object + a string containing the name of the function to connect to.

Simple example:
class Controller : Object
{
Controller()
{
SIGNAL_KeyPressed.connect(this, "handleKeyPress");
}
void handleKeyPress(KeyCode key)
{
if(key == Space)
maChest.activation();
}
};


Thats quite acceptable to me.


Wow... didn't think of that one there. That is perfect for what I need.

This topic is closed to new replies.

Advertisement