AngelScript function pointers now working

This topic is 3128 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys, I'd just wanted to let you know that I've finally been able to get to the point where function pointers are now fully working. I welcome those that have been waiting for this feature to give it a try so I can root out as many bugs as possible before I release version 2.18.2. Here's a script example that shows the syntax for the function pointers:
  // Define a function signature for the function pointer
funcdef bool CALLBACK(int, int);

// An example function that to use this
void main()
{
// Declare a function pointer, and set it
// to point to the myCompare function.
CALLBACK @func = @myCompare;

// The function pointer can be compared with the 'is' operator
if( func is null )
{
print("The function pointer is null\n");
return;
}

// Call the function through the pointer, just as if it was a normal function
if( func(1, 2) )
{
print("The function returned true\n");
}
else
{
print("The function returned false\n");
}
}

// This function matches the CALLBACK definition, since it has
// the same return type and parameter types.
bool myCompare(int a, int b)
{
return a > b;
}

One disclaimer though, while the function pointers are now fully working, they are still a bit limited in their usefulness as there is still no way for the application to define the function definitions, this means that the function pointers can really only be used within scripts, but not be passed to or from the application. This is a limitation that I'll address with version 2.19, when I'll update the application interface. There are also some other limitations that I plan on improving over the coming releases. For example, the addition of a generic function pointer type that can hold any function pointer, regardless of signature, and the addition of reference casts for this generic type to the real function pointer type. This will make it much more versatile. Regards, Andreas

Share on other sites
Can this version of function pointers be used in import declarations? And similarly, will imports be treated as function pointers eventually?

Share on other sites
I tried running the example in your post, on svn revision 561, and I get the error
script (5, 1) : INFO : Compiling void main()
script (9, 11) : ERR : Expected ';'
and line 9 is
CALLBACK @func = @myCompare;

Edit:
I ran the features test and everything passed...

Edit 2:
If the line
CALLBACK @func = @myCompare;
gets moved out side of main, it works.

Share on other sites
Quote:
 Original post by Thy ReaperCan this version of function pointers be used in import declarations? And similarly, will imports be treated as function pointers eventually?

Not yet, but the idea is that the imports will eventually be replaced with function pointers. Probably the CScriptBuilder will inherit the automatic binding of the imports as this is taken out of the core engine.

Share on other sites
Quote:
 Original post by klusarkI tried running the example in your post, on svn revision 561, and I get the error

Just fixed this in revision 562. Thanks.

This is exactly why I want you guys to test this.

Regards,
Andreas

Share on other sites
I got a crash! Here is the script. What I am doing is probably not intended :P

funcdef bool CALLBACK(int, int);funcdef bool CALLBACK3(CALLBACK @, CALLBACK @);funcdef bool CALLBACK2(CALLBACK @, CALLBACK3 @);void main(){	CALLBACK @func = @myCompare;	CALLBACK2 @func2 = @test;	CALLBACK3 @func3 = @test2;	func2(func, func3);}bool test(CALLBACK @func, CALLBACK3 @func2){	if( func(1, 2) )	{		print("The function returned true\n");	}	else	{		print("The function returned false\n");	}	return true;}bool test2(CALLBACK @func, CALLBACK @func2){	if( func(1, 2) )	{		print("The function returned true\n");	}	else	{		print("The function returned false\n");	}	return true;}bool myCompare(int a, int b){	return a > b;}

Share on other sites
Fixed in revision 563. Thanks.

Keep on testing :)

Share on other sites
Got another crash! I also tried using recursive functions, but that did not crash.
funcdef bool CALLBACK(int, int);class testclass{	CALLBACK @func;	testclass()	{		@func = @myCompare;		func(2,3);	}}void main(){	testclass test;}bool myCompare(int a, int b){	testclass @testc = @testclass();	return a < b;}

Edit: I dont think that is a function pointer bug. Here is another example that crashes
class testclass{	testclass()	{		myCompare(1,3);	}}void main(){	testclass test;}bool myCompare(int a, int b){	testclass @testc = @testclass();	return a < b;}

[Edited by - klusark on February 25, 2010 12:42:31 PM]

Share on other sites
doesn't it look a little recursive? how'd it stop?

Share on other sites
That is the thing, it crashes before it gets started.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
14
5. 5

• 10
• 9
• 16
• 9
• 9
• Forum Statistics

• Total Topics
632593
• Total Posts
3007304

×