AngelScript

Function pointers

A function pointer is a data type that can be dynamically set to point to a global function that has a matching function signature as that defined by the variable declaration. Function pointers are commonly used for callbacks, i.e. where a piece of code must be able to call back to some code based on some conditions, but the code that needs to be called is not known at compile time.

To use function pointers it is first necessary to define the function signature that will be used at the global scope. Once that is done the variables can be declared using that definition.

Here's an example that shows the syntax for using function pointes:

      // Define a function signature for the function pointer
      funcdef bool CALLBACK(int, int);

      // An example function that shows how 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;
      }