Skip to content

Components

a cartoon fennec with a hand truck moving a large stack of colored boxes

data, relationships, and meaning – packed neatly in boxes Neofox: box

Neofox: thumbsup The Building Blocks of Your Entities

Components are data attached to Entities! They're how you give entities properties, behaviors, and relationships. From simple values to complex relations – components make your game world come alive.

What is a Component?

A Component is any piece of data attached to an Entity. In fennecs, components can be value types, reference types, tags, or even relationships to other entities and objects!

cs
// Simple data component
entity.Add(new Position { X = 10, Y = 20 });

// Tag component (zero-size marker)
entity.Add<Enemy>();

// Relation to another entity
entity.Add<ChildOf>(parentEntity);

// Link to a managed object
entity.Add(Link.With(gameObject));

Whenever a Component is added or removed, the Entity moves to a new Archetype.

Quick Reference

Component TypeDescriptionDocumentation
Value TypesStructs stored contiguously – fast & cache-friendlyValues
TagsZero-size markers for categorizationTags
ShareablesReference types shared between entitiesShareables
RelationsEntity-to-entity relationshipsRelations
Object LinksLinks to managed objectsLinks
ExpressionsMeta-level component referencesExpressions

Component Rules Neofox: comfy

Entities can have:

  1. Zero or one of each Component Type Expression (Type + Relation Target)
  2. Any number of components (as long as their Type Expressions are unique)

Neofox: science Type Expressions

A TypeExpression combines a component's backing type with an optional relation target. This means you can have multiple components of the same type if they have different targets!

Examples

cs
// Plain Data Components - The most common case
record struct Cash(decimal Amount);
record struct Height(float Meters);

bob.Add<Cash>(new(3.25M)); // bob has $3 and 25 cents
bob.Add(new Height(1.85f)); // bob is 1.85 meters tall
cs
// Components can carry a Relation to another Entity
record struct Owes(decimal Amount);

bob.Add<Owes>(new(10M), alice); // he owes alice $10
bob.Add<Owes>(new(23M), eve);   // and owes eve $23

// Each relation is a separate component!
bob.Ref<Owes>(eve).Amount += 7M; // modify eve's balance
cs
// Reference types can be shared between Entities
Bank chase = new("Chase"); // Bank is a class!

// Many entities can reference the same instance
eve.Add<Bank>(chase);
bob.Add<Bank>(chase);
// Both now share the same Bank object
cs
// Components can BE an object (the link IS the data)
bob.Add(Link.With(chase)); // bob banks at chase
bob.Add(Link.With(targo)); // bob also banks at targo
// Two Bank relations, each backed by the object itself

What Components Can Do Neofox: heart

CapabilityDescription
Add/RemoveAttach or detach from entities (single or bulk)
Any TypeBacked by any C# language type
RelationsHave an optional Target as secondary key
SharingReference types can be shared between entities
QueriesQueried and processed by Streams

Archetypes

Neofox: packed fennecs is an Archetype-Based ECS

Archetypes are internal collections of Entities that share the same set of Components. As an ECS user, you never interact with Archetypes directly, but understanding them helps you get the most out of fennecs.

By grouping Entities into Archetypes, the ECS (and by extension the CPU) can process them very efficiently.

fennecs constantly ensures that Entities with the same Components are stored tightly packed together in memory, which is a key factor in achieving high performance.

Neofox: packed_blue Neofox: packed Neofox: packed_green

Neofox: science Performance Tip

In large projects, aim for either:

  • Few, small archetypes (dozens of entities)
  • Large, chunky archetypes (10k+ entities)

This maximizes cache efficiency during iteration.

Neofox: magnify Archetype Internals

There's no practical limit to Archetypes and their sizes. Archetypes can get compacted or removed by the World's garbage collector.

This is especially true for Archetypes of Relation Targets when the target despawns – because that Archetype is extremely unlikely to ever get any Entities again.

fennecs is released under the MIT License. Neofox is released under the CC BY-NC-SA 4.0 License.