Flecs.NET

repository·main·Indexed 42 days ago

https://github.com/beancheeseburrito/flecs.net

A high-level C# wrapper for the flecs ECS providing a modern .NET 9 API with near feature parity to the C++ version. It includes low-level C API bindings and precompiled native libraries for multiple platforms, supporting record struct components for minimal GC interaction and static linking for PublishAOT.

Tokens
1.2K
Snippets
5
Records
8
Agent score
4%

What's inside Flecs.NET

  1. Install Flecs.NET via NuGet

    main

    You can install the full wrapper (including bindings and native libraries) using the following command. It is recommended to use the Debug package during development to enable API usage checks.

    dotnet add PROJECT package Flecs.NET.Release --version *-*
  2. Build Flecs.NET from source

    main

    To build the project from source, follow these steps:

    1. Clone the repo recursively: git clone --recursive https://github.com/BeanCheeseBurrito/Flecs.NET.git
    2. Restore dependencies: dotnet restore
    3. Build: dotnet build. This will automatically download the Zig compiler and cross-compile native libraries for Linux, macOS, and Windows.
  3. Access development builds from GitHub Package Registry

    main

    To use the latest development builds, add the GitHub package source using a personal access token with read:packages permission.

    Add source globally:

    dotnet nuget add source --name "flecs.net" --username "YOUR_GITHUB_USERNAME" --password "YOUR_GITHUB_TOKEN" --store-password-in-clear-text "https://nuget.pkg.github.com/BeanCheeseBurrito/index.json"

    Add source to a specific project/solution:

    dotnet nuget add source --configfile "./nuget.config" --name "flecs.net" --username "YOUR_GITHUB_USERNAME" --password "YOUR_GITHUB_TOKEN" --store-password-in-clear-text "https://nuget.pkg.github.com/BeanCheeseBurrito/index.json"

    Remove the source:

    dotnet nuget remove source "flecs.net"
  4. Configure Flecs.NET package references for Debug and Release

    main

    To automatically switch between Debug (with usage checks) and Release packages based on your build configuration, use the following XML pattern in your .csproj file:

    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>net9.0</TargetFramework>
        </PropertyGroup>
    
        <ItemGroup>
            <PackageReference Include="Flecs.NET.Debug" Version="*-*" Condition="'$(Configuration)' == 'Debug'" />
            <PackageReference Include="Flecs.NET.Release" Version="*-*" Condition="'$(Configuration)' == 'Release'" />
        </ItemGroup>
    </Project>
  5. Enable Static Linking for PublishAOT

    main

    If you are using PublishAOT, you can enable static linking by setting <FlecsStaticLink>true</FlecsStaticLink>. To prevent shared libraries from being copied to your output directory, add ExcludeAssets="native" to your package reference.

    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
            <PublishAot>true</PublishAot>
            <FlecsStaticLink>true</FlecsStaticLink>
        </PropertyGroup>
    
        <ItemGroup>
            <PackageReference Include="Flecs.NET.Debug" ExcludeAssets="native"/>
        </ItemGroup>
    </Project>
  6. Quickstart with Flecs.NET

    main

    Flecs.NET is a high-level C# wrapper for the flecs ECS. You can create a world, spawn entities with components, and run systems using the world.Each method. Components can be defined as record struct types for minimal GC interaction.

    using Flecs.NET.Core;
    
    using World world = World.Create();
    
    Entity entity = world.Entity()
        .Set(new Position(10, 20))
        .Set(new Velocity(1, 2));
    
    world.Each(static (ref Position p, ref Velocity v) =>
    {
        p.X += v.X;
        p.Y += v.Y;
    });
    
    public record struct Position(float X, float Y);
    public record struct Velocity(float X, float Y);
  7. Regenerate Bindings and Codegen

    main

    If you are developing on Flecs.NET and need to update the generated code:

    • Regenerate C API Bindings: dotnet run --project src/Flecs.NET.Bindgen
    • Rerun Code Generators: dotnet run --project src/Flecs.NET.Codegen (outputs to the Generated folder in the Flecs.NET project).