Burst 1.5.0 and Generic Function Pointers

In this page of the documentation, there’s a line that says “Function pointers don’t support generic delegates.” This is partially correct. The truth is that generic FunctionPointers run in a compiled Job DO work; however, if the FunctionPointer is run outside of the job (via mono), they DO NOT work.

In Burst version 1.5.0, there’s a nifty feature where you can assume booleans to be true or false. Using that, you can make your code do different things if it’s compiled by Burst or not.

Here’s how you use the Assume method to create a FunctionPointer implementation that supports generics in all cases:

You have 2 cases – When the code being run is compiled by Burst, then you use the generic FunctionPointer. When the code being run isn’t compiled by burst, then you use a stored pointer of the C# delegate. All you have to do is keep a map from the function pointer to the C# delegate. You can do this when you compile the FunctionPointer.

There ya go, now it’s much easier to define FunctionPointers in your generic methods and structs.

6 Likes

I’ve been using Burst version 1.6.x, and this no longer works. Getting an error that generic functions aren’t supported.

1 Like