How to load components and systems from DLL?

I want to load a DLL and add its components and systems to the world for updating. Additionally, I want to synchronize the [GhostField] annotated fields in the components for snapshot generation. Do you have a solution for this?

Hey hxhloveunity! Just to clarify: I assume you mean; load a DLL at runtime in a player build? E.g. On Windows?
Because, loading a DLL in the editor should “just work”.

I’m unsure (and have asked internally) whether or not Entities supports this via the TypeManager (which tries to avoid reflection where possible), nor am I sure if it’ll “just work” if you manually add these DLL-loaded systems to a world (also asked).

The one thing that I’m certain will not work is the [GhostField] annotations, which require editor-time code-generation. However, you can work around this by implementing a [GhostField] IBufferElementData of byte data, then using your own serialize/deserialize methods (using DataStreamReader/Writer to write into this DynamicBuffer).

Example:

/// <summary>
/// Allows a type loaded at runtime (via DLL) to be sent over the network (via <see cref="DynamicTypesSerializedData"/>).
/// </summary>
public interface ISerializableDynamicType
{
    void Write(ref DataStreamWriter writer, ref StreamCompressionModel model);
    void Read(ref DataStreamReader reader, ref StreamCompressionModel model);
}

/// <summary>
/// <see cref="ISerializableDynamicType"/>.
/// </summary>
public struct DynamicTypesSerializedData : IBufferElementData
{
    [GhostField]
    public byte Data;
}
2 Likes

If the dlls are found before TypeManager.Initialize is called, we will load those dlls and find all the relevant ECS types. However once TypeManager.Initialize has been called, we do not append new types to the TypeManager today. Hope this helps.

1 Like

To further clarify:
The TypeManager.Initialize is called when the first World is created. And this happen the the automatic bootstrap run (for Hybrid).
If you disable the automatic bootstrapping or you write you own custom bootstrap that load the DLLs before the default world is created, your types and system are going to be considered.

But for neither of Netcode nor Entities and System in general, nothing will work as expected because of code-gen. SystemAPI and ghost serialization requires ILPostProcessing and SourceGenerators.

If you built the dll using the AsssemblyBuilder API in the editor or by passing all the SourceGenerators, ILPostprocessor and passing the correct additional files to the build process (i.e MSBuild) then things can can work.

1 Like