Skip to content

🦆 Generating code

Isadora edited this page Sep 28, 2023 · 5 revisions

When profiling and looking for areas of improvements, we ended up going for a metadata-based code generation of the components. When using Bang, it expects that an implementation exists somewhere in your game of ComponentsLookup.

This maps each component to an index, used when storing components in your entities, see example.

The generator that for Bang works by adding a reference to the project with:

  <ItemGroup>
    <!-- Pretty analyzers! -->
    <ProjectReference Condition="'$(Configuration)' == 'Debug'" Include="..\..\murder\bang\src\Bang.Analyzers\Bang.Analyzers.csproj">
      <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
      <OutputItemType>Analyzer</OutputItemType>
    </ProjectReference>

    <!-- And generators! -->
    <ProjectReference Include="..\..\murder\bang\src\Bang.Generator\Bang.Generator.csproj">
      <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
      <OutputItemType>Analyzer</OutputItemType>
    </ProjectReference>
  </ItemGroup>

...which scans all your assemblies and generate the components information according to its types into generated files:

\<ProjectName>ComponentsLookup.g.cs
\<ProjectName>ComponentTypes.g.cs
\<ProjectName>EntityExtensions.g.cs
\<ProjectName>MessageTypes.g.cs
Clone this wiki locally