Hello! Is there some way to pass a method address as an argument to a native function?
I've read about delegates, but they need to be bound to an object.
Passing a method address as a function argument
If I remember correctly, AngelScript lacks of something equivalent to member pointer in C++.
But maybe you can use anonymous function to achieve this? It can wrap a method as global function, then pass it to native function using asIScriptFunction*
to receive it (ref: AngelScript: Funcdefs and script callback functions).
class foo
{
void f1(int i) { print("f1"); }
void f2(int i) { print("f2"); }
};
funcdef void memfn(foo@ this_, int i);
memfn@ my_cb;
// Use script function just for showing the logic.
// You can implement it as native funciton for your own program
void set_cb(memfn@ f)
{
@my_cb = @f;
}
void main()
{
foo obj;
set_cb(function(foo@ f, int i) { f.f1(i); });
my_cb(obj, 0); // Called foo::f1
}
None
That's correct. The only way to pass a class method from the script to the application is as a delegate, which requires an active object instance. If you only want to store the class method, you can retrieve the method from the delegate with GetDelegateFunction and then release the delegate.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game