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

Question How to generate a static readonly array on startup of the game, and access it with JOBS?

Discussion in 'C# Job System' started by morphex, May 8, 2023.

  1. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    108
    I am trying to generate a Lookup Table on a static class, that I want to be able to access from inside ANY job.

    This will never change during runtime, but needs to be generate on the fly:

    Things I have tried:



    Example 1 : Burst Compiler complains it cant access managed array

    public static class LUTHolder {

    public readonly static int3[] LUT = generateLUT();

    public generateLUT() { return new int3[]}

    }


    Example 2 : Burst Compiler complains it cant access managed array

    public static class LUTHolder {

    public readonly static int3[] LUT = new int3[];

    static LUTHolder {
    // generate the lut here
    LUT[0] = new int3(0,1,0);
    }

    }


    Example3 : LUT is empty none of the values I put in are inside jobs, it looks like JOBS is copying the array while its empty.

    public static class LUTHolder {

    public readonly static int3[] LUT = new int3[];

    public static void Generate() {
    // generate the lut here
    LUT[0] = new int3(0,1,0);
    }

    }


    In my code the first thing I do before scheduling the job for the first time:

    LUTHOLDER.Generate();


    Things I dont want:

    • I dont want to pass my LUT to all my JOBS (there are loads of LUTS and Jobs), unless there is 100% not another way.
    • I dont want to pre-generate the LUT before compiling the game.

    Thank you!
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    Static readonly arrays are not what you want if you want to generate the data at runtime. SharedStatic is probably what you want.
     
  3. morphex

    morphex

    Joined:
    Dec 18, 2012
    Posts:
    108
    You know, sometimes, I just get locked in a specific problem and forget everything else that exists in the ecosystem.
    I used SharedStatics in the project, but I was so focused on - this is essencially a non mutable array after generation and got locked into this mindset that I completly forgot about the SharedStatic.

    Thank you @DreamingImLatios. This is the third time you have helped me :)