Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

funcdefs to member functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Mr. Beast   Members   -  Reputation: 103

Like
0Likes
Like

Posted 31 December 2010 - 02:56 AM

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

Sponsor:

#2 CodeStorm   Members   -  Reputation: 127

Like
0Likes
Like

Posted 31 December 2010 - 03:14 AM

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!

#3 Andreas Jonsson   Moderators   -  Reputation: 3454

Like
0Likes
Like

Posted 09 January 2011 - 06:42 PM

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

#4 Mr. Beast   Members   -  Reputation: 103

Like
0Likes
Like

Posted 11 January 2011 - 06:53 AM

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.


#5 _orm_   Members   -  Reputation: 112

Like
0Likes
Like

Posted 14 January 2011 - 10:57 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS