Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Burst 1.5.0 and Generic Function Pointers

Discussion in 'Entity Component System' started by jasonboukheir, Mar 10, 2021.

  1. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    80
    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
    FunctionPointer
    s 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:

    https://github.com/CareBoo/Burst.De...ates/CareBoo.Burst.Delegates/BurstFunc.gen.cs

    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
    FunctionPointer
    s in your generic methods and structs.
     
    Neiist, bb8_1, quabug and 3 others like this.
  2. jasonboukheir

    jasonboukheir

    Joined:
    May 3, 2017
    Posts:
    80
    I've been using Burst version 1.6.x, and this no longer works. Getting an error that generic functions aren't supported.

     
    Neiist likes this.