• Create Account

### #ActualAndreas Jonsson

Posted 07 April 2013 - 03:00 PM

I've just checked in the last changes to get delegates fully working in the AngelScript library.

If you're interested in trying it out, then you'll need to download revision 1613 from the SVN

The solution builds upon the existing function pointers in AngelScript, and the syntax for creating delegates is similar to C#'s delegates.

class Foo
{
void Bar() { counter++; }
void counter = 0;
}

// Declare a funcdef, which is basically the desired signature for the delegates
funcdef void CALLBACK();

void main()
{
Foo f;

// Create the delegate as a construct call for the funcdef
// Observe that the single argument gives the object reference and the name of the method
CALLBACK @callback = CALLBACK(f.Bar);

// Invoke the callback like a normal function
callback();
}


The delegate is an actual object instance, which is slightly different from function pointers to global functions that doesn't create any new object instances. Even so delegates and function pointers are interchangeable as long as the function signature is identical.

Delegates cannot be created for value types as the delegate need to be able to hold on to a reference to the object. Currently it is not possible to create delegates for global functions either, but I'll probably have that implemented in the near future.

I hope you like the new enhancement. And please let me know if you find any problems with the implementation, so I can preferably have them fixed before the next release.

Regards,

Andreas

### #3Andreas Jonsson

Posted 07 April 2013 - 02:59 PM

I've just checked in the last changes to get delegates fully working in the AngelScript library.

If you're interested in trying it out, then you'll need to download revision 1613 from the SVN

The solution build's upon the existing function pointers in AngelScript, and the syntax for creating delegates is similar to C#'s delegates.

class Foo
{
void Bar() { counter++; }
void counter = 0;
}

// Declare a funcdef, which is basically the desired signature for the delegates
funcdef void CALLBACK();

void main()
{
Foo f;

// Create the delegate as a construct call for the funcdef
// Observe that the single argument gives the object reference and the name of the method
CALLBACK @callback = CALLBACK(f.Bar);

// Invoke the callback like a normal function
callback();
}


The delegate is an actual object instance, which is slightly different from function pointers to global functions that doesn't create any new object instances. Even so delegates and function pointers are interchangeable as long as the function signature is identical.

Delegates cannot be created for value types as the delegate need to be able to hold on to a reference to the object. Currently it is not possible to create delegates for global functions either, but I'll probably have that implemented in the near future.

I hope you like the new enhancement. And please let me know if you find any problems with the implementation, so I can preferably have them fixed before the next release.

Regards,

Andreas

### #2Andreas Jonsson

Posted 07 April 2013 - 02:58 PM

I've just checked in the last changes to get delegates fully working in the AngelScript library.

If you're interested in trying it out, then you'll need to download revision 1613 from the SVN

The solution build's upon the existing function pointers in AngelScript, and the syntax for creating delegates is similar to C#'s delegates.

class Foo
{
void Bar() { counter++; }
void counter = 0;
}

// Declare a funcdef, which is basically the desired signature for the delegates
funcdef void CALLBACK();

void main()
{
Foo f;

// Create the delegate as a construct call for the funcdef
// Observe that the argument gives the name of the class method
CALLBACK @callback = CALLBACK(f.Bar);

// Invoke the callback like a normal function
callback();
}


The delegate is an actual object instance, which is slightly different from function pointers to global functions that doesn't create any new object instances. Even so delegates and function pointers are interchangeable as long as the function signature is identical.

Delegates cannot be created for value types as the delegate need to be able to hold on to a reference to the object. Currently it is not possible to create delegates for global functions either, but I'll probably have that implemented in the near future.

I hope you like the new enhancement. And please let me know if you find any problems with the implementation, so I can preferably have them fixed before the next release.

Regards,

Andreas

### #1Andreas Jonsson

Posted 07 April 2013 - 02:52 PM

I've just checked in the last changes to get delegates fully working in the AngelScript library.

If you're interested in trying it out, then you'll need to download revision 1613 from the SVN

The solution build's upon the existing function pointers in AngelScript. And the syntax for creating delegates is similar to C#'s delegates or C++' std::mem_fn.

class Foo
{
void Bar() { counter++; }
void counter = 0;
}

// Declare a funcdef, which is basically the desired signature for the delegates
funcdef void CALLBACK();

void main()
{
Foo f;

// Create the delegate as a construct call for the funcdef
// Observe that the argument gives the name of the class method
CALLBACK @callback = CALLBACK(f.Bar);

// Invoke the callback like a normal function
callback();
}


The delegate is an actual object instance, which is slightly different from function pointers to global functions that doesn't create any new object instances. Even so delegates and function pointers are interchangeable as long as the function signature is identical.

Delegates cannot be created for value types as the delegate need to be able to hold on to a reference to the object. Currently it is not possible to create delegates for global functions either, but I'll probably have that implemented in the near future.

I hope you like the new enhancement. And please let me know if you find any problems with the implementation, so I can preferably have them fixed before the next release.

Regards,

Andreas

PARTNERS