paul_ecs.h

Includes:
<stdint.h>
<stddef.h>
<Block.h>
<stdlib.h>
<stdarg.h>
<string.h>
<assert.h>

Introduction

Entity Component System (ECS) library for C/C++.

Discussion

Implementation is included when PAUL_ECS_IMPLEMENTATION or PAUL_IMPLEMENTATION is defined.

Updated:
Monday, September 29, 2025


Functions

ecs_component

Create a new component

ecs_delete

Delete an entity

ecs_find

Find entities matching component requirements

ecs_query

Query entities and execute a function on each

ecs_spawn

Create a new entity

ecs_step

Execute all systems in the world

ecs_system

Create a new system

ecs_world

Create a new ECS world

ecs_world_destroy

Destroy an ECS world

entity_cmp

Compare two entities

entity_get

Get component data from an entity

entity_give

Give a component to an entity

entity_has

Check if an entity has a component

entity_isa

Check if an entity is of a specific type

entity_isnil

Check if an entity is nil

entity_isvalid

Check if an entity is valid

entity_remove

Remove a component from an entity

entity_set

Set component data for an entity


ecs_component


Create a new component

entity_t ecs_component(
    world_t *world,
    size_t size_of_component);  
Parameters
world

The ECS world

size_of_component

Size of the component data

Return Value

Returns a new component entity


ecs_delete


Delete an entity

void ecs_delete(
    world_t *world,
    entity_t e);  
Parameters
world

The ECS world

e

The entity to delete


ecs_find


Find entities matching component requirements

entity_t* ecs_find(
    world_t *world,
    filter_system_t filter,
    int *result_count,
    int component_count,
    ...);  
Parameters
world

The ECS world

filter

Optional filter function

result_count

Pointer to store the number of results

component_count

Number of component types

...

Component types to match

Return Value

Returns an array of matching entities


ecs_query


Query entities and execute a function on each

void ecs_query(
    world_t *world,
    system_t fn,
    filter_system_t filter,
    int component_count,
    ...);  
Parameters
world

The ECS world

fn

The function to call for each matching entity

filter

Optional filter function

component_count

Number of component types

...

Component types to match


ecs_spawn


Create a new entity

Parameters
world

The ECS world

Return Value

Returns a new entity


ecs_step


Execute all systems in the world

void ecs_step(
    world_t *world);  
Parameters
world

The ECS world


ecs_system


Create a new system

entity_t ecs_system(
    world_t *world,
    system_t fn,
    int component_count,
    ...);  
Parameters
world

The ECS world

fn

The system function

component_count

Number of component types

...

Component types the system operates on

Return Value

Returns a new system entity


ecs_world


Create a new ECS world

world_t* ecs_world(
    void);  
Return Value

Returns a new ECS world


ecs_world_destroy


Destroy an ECS world

void ecs_world_destroy(
    world_t **world);  
Parameters
world

Pointer to the world to destroy


entity_cmp


Compare two entities

Parameters
a

First entity

b

Second entity

Return Value

Returns non-zero if entities are equal


entity_get


Get component data from an entity

void* entity_get(
    world_t *world,
    entity_t e,
    entity_t c);  
Parameters
world

The ECS world

e

The entity

c

The component

Return Value

Returns a pointer to the component data or NULL


entity_give


Give a component to an entity

void* entity_give(
    world_t *world,
    entity_t e,
    entity_t c);  
Parameters
world

The ECS world

e

The entity

c

The component

Return Value

Returns a pointer to the component data


entity_has


Check if an entity has a component

int entity_has(
    world_t *world,
    entity_t e,
    entity_t c);  
Parameters
world

The ECS world

e

The entity

c

The component

Return Value

Returns non-zero if the entity has the component


entity_isa


Check if an entity is of a specific type

int entity_isa(
    world_t *world,
    entity_t e,
    int type);  
Parameters
world

The ECS world

e

The entity to check

type

The type to check against

Return Value

Returns non-zero if the entity is of the given type


entity_isnil


Check if an entity is nil

Parameters
e

The entity to check

Return Value

Returns non-zero if the entity is nil


entity_isvalid


Check if an entity is valid

int entity_isvalid(
    world_t *world,
    entity_t e);  
Parameters
world

The ECS world

e

The entity to check

Return Value

Returns non-zero if the entity is valid


entity_remove


Remove a component from an entity

void entity_remove(
    world_t *world,
    entity_t e,
    entity_t c);  
Parameters
world

The ECS world

e

The entity

c

The component


entity_set


Set component data for an entity

void entity_set(
    world_t *world,
    entity_t e,
    entity_t c,
    void *data);  
Parameters
world

The ECS world

e

The entity

c

The component

data

The data to set


Constants

ecs_nil
ecs_nil_entity

ecs_nil


extern const uint64_t ecs_nil;  
Discussion

Nil entity ID value


ecs_nil_entity


extern const entity_t ecs_nil_entity;  
Discussion

Nil entity value


Typedefs

entity_t
filter_system_t
system_t
world_t

entity_t


typedef union entity { 
    struct { 
        uint32_t id; 
        uint16_t version; 
        uint8_t alive; 
        uint8_t type; 
        }; 
    uint64_t value; 
} entity_t;  
Fields
id

The entity ID

version

The version number

alive

Whether the entity is alive

type

The entity type

value

The combined 64-bit value

Discussion

Entity union representing ECS entities


filter_system_t


typedef int( *filter_system_t)(
    entity_t);  
Parameters
entity

The entity to filter

Return Value

Returns non-zero if the entity matches the filter

Discussion

Filter system function type


system_t


typedef void( *system_t)(
    entity_t);  
Parameters
entity

The entity to process

Discussion

System function type


world_t


typedef struct world world_t;  
Discussion

ECS world structure


Enumerated Types

ECS

ECS


Constants
ECS_ENTITY

Entity type

ECS_COMPONENT

Component type

ECS_SYSTEM

System type

Discussion

entity types