Can't run burst code loaded via DLL at runtime

I’m trying to create a moddable game, where mods provide new code (both managed and burst). Here’s the code in the mod

[BurstCompile]
public class BurstTest : MonoBehaviour
{

    public TMP_Text text;
    private int counter = 0;

    void Update()
    {
        counter++;
        int result = GetTESTNum(counter);
        text.text = "Result: " + result;
    }

    [BurstCompile]
    private static int GetTESTNum(int input) 
    {
        int result = input * 2; 
        result += 6; 
        result = result / 2;
        return result; //whatever, I just want to PoC that it runs in burst
    }
}

This runs fine if I’m in the mod project, but if I’m in my main project, where I load this in via DLL, I get errors.

Below is the export/import method. I’ve used this build process for non-burst mods/dlcs for years and never had issue, but I can’t make it work for burst

Exporting:

        var bpo = new BuildPlayerOptions()
        {
            locationPathName = Path.Combine(destination, "__build", "plugin"),
            target = BuildTarget.StandaloneWindows64,
            options = BuildOptions.BuildScriptsOnly, 
        };

        BuildPipeline.BuildPlayer(bpo);

This gives me a build folder with:
My managed DLL in plugin_Data\Managed
A lib_burst_generated.dll in plugin_Data\Plugins\x86_64

Importing

        AssemblyUtility.registerAssembly(managedDLLPath);
        BurstRuntime.LoadAdditionalLibrary(burstDLLPath);

Seems to work fine, at least, doesn’t throw any exceptions. However, I get a whole bunch of exceptions from my GetTESTNum method, below:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <b11ba2a8fbf24f219f7cc98532a11304>:0)
Burst.Compiler.IL.Jit.JitCompilerService.CompileILPPMethod (System.String args) (at <69b504cf34c54d688310e60f1ce29d2e>:0)
Burst.Compiler.IL.Jit.JitCompilerService.ProcessCommand (System.String commandName, System.String args) (at <69b504cf34c54d688310e60f1ce29d2e>:0)
Burst.Compiler.IL.Jit.JitCompilerService.CompileInternal (System.String fullMethodName, System.String assemblyPaths, System.IntPtr userdata, Unity.Burst.NativeDumpFlags dumpFlags, System.IntPtr compilerCallbackPointer, System.IntPtr logCallBack, System.String compilerFlags) (at <69b504cf34c54d688310e60f1ce29d2e>:0)
Unity.Burst.LowLevel.BurstCompilerService:GetDisassembly(MethodInfo, String)
Unity.Burst.BurstCompiler:SendRawCommandToCompiler(String)
Unity.Burst.CommandBuilder:SendToCompiler()
Unity.Burst.BurstCompiler:GetILPPMethodFunctionPointer2(IntPtr, RuntimeMethodHandle, RuntimeTypeHandle)
GetTESTNum_00000002$BurstDirectCall:GetFunctionPointerDiscard(IntPtr&)

followed by

Assertion failed on expression: 'exception == SCRIPTING_NULL'
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Unity.Burst.BurstCompiler:SendRawCommandToCompiler (string) (at ./Library/PackageCache/com.unity.burst@1.8.18/Runtime/BurstCompiler.cs:787)
Unity.Burst.BurstCompiler/CommandBuilder:SendToCompiler () (at ./Library/PackageCache/com.unity.burst@1.8.18/Runtime/BurstCompiler.cs:92)
Unity.Burst.BurstCompiler:GetILPPMethodFunctionPointer2 (intptr,System.RuntimeMethodHandle,System.RuntimeTypeHandle) (at ./Library/PackageCache/com.unity.burst@1.8.18/Runtime/BurstCompiler.cs:284)
BurstTest/GetTESTNum_00000002$BurstDirectCall:GetFunctionPointerDiscard (intptr&)

Things I’ve checked already

  • Yes, burst compile is enabled on the mod project, it’s generating a burst dll after all
  • Apparently burst doesn’t like working in file paths with non-ascii chars, checked for this already
  • To reiterate, the above code works fine when run from the mod project (so, no importing of DLLs at runtime), I’ve also used the burst inspector to confirm that the GetTESTNum method is in fact running on burst
  • Tried changing the order of the DLL loading, doesn’t seem to make a difference if managed is loaded before burst, or vice-versa
  • Tried changing the name of the burst DLL to something different, and loading it under a new name (thought maybe it was conflicting with another burst DLL that was already loaded, idk)
  • Updated to most recent version of burst (both in the mod and in the main codebase)

All out of ideas! At this point, I don’t know if the problem is in the export side, the import side, or both! If anyone has any thoughts, any help would be super appreciated, I’d love to get something working this week :smiling_face_with_tear:

Finally got this working

  • Mod’s DLL must be renamed to something specific to that mod. If it has the default “burst_lib_generated” name, it wont actually be loaded, as the main codebase already has one. Helpfully, this fails silently, rather than give me some descriptive error like “assembly already registered, skipping”
  • None of this works in editor, the main project needs to be built before it’ll play nice with my burst mod (although that may be an ECS thing rather than a burst thing)
1 Like

Hi @Tovey-Ansell, I’m glad you got it working.

None of this works in editor, the main project needs to be built before it’ll play nice with my burst mod (although that may be an ECS thing rather than a burst thing)

You’ve probably run into an issue that specifically affects Burst-compiled static methods, in mods, that are loaded in the Editor. You might find that if you were to instead use jobs (e.g. [BurstCompile] struct MyJob : IJob), then you’d be able to load your mod dll in the Editor.

Loading systems from mod DLL in editor not working for another reason no Burst involved here :smiley: (link to Unity discord thread, second response started from But in addition there is another issue here - UnityEditor.TypeCache)

1 Like