This document provides a comprehensive reference for the Coyote ECS API, including all major functions, structs, and types.
The World struct is the main container for all entities, components, and systems in the ECS.
create() !*WorldCreates a new ECS world.
var world = try World.create();
defer world.destroy();
destroy(self: *World) voidDestroys the world and all its entities and components.
world.destroy();
The Entity struct represents an entity in the ECS.
create(ctx: *SuperEntities) !*EntityCreates a new entity.
const entity = try world.entities.create();
destroy(self: *Entity) voidDestroys the entity.
entity.destroy();
addComponent(ctx: *Entity, comp_val: anytype) !*ComponentAdds a component to the entity.
const component = try entity.addComponent(MyComponent{ .value = 42 });
getOneComponent(ctx: *Entity, comptime comp_type: type) ?*const ComponentGets a component of the specified type from the entity.
if (entity.getOneComponent(MyComponent)) |component| {
// Use component
}
attach(self: *Entity, component: *Component, comp_type: anytype) !voidAttaches a component to the entity.
try entity.attach(component, MyComponent{ .value = 42 });
detach(self: *Entity, component: *Component) !voidDetaches a component from the entity.
try entity.detach(component);
set(self: *Entity, component: *Component, comptime comp_type: type, members: anytype) !voidSets the values of a component.
try entity.set(component, MyComponent, .{ .value = 42 });
The Component struct represents a component in the ECS.
is(self: *const Component, comp_type: anytype) boolChecks if the component is of the specified type.
if (component.is(MyComponent)) {
// Component is of type MyComponent
}
set(component: *Component, comptime comp_type: type, members: anytype) !voidSets the values of the component.
try component.set(MyComponent, .{ .value = 42 });
detach(self: *Component) voidDetaches the component from all entities.
component.detach();
dealloc(self: *Component) voidDeallocates the component’s data.
component.dealloc();
destroy(self: *Component) voidDestroys the component.
component.destroy();
The SuperComponents struct manages all components in the ECS.
count(ctx: *SuperComponents) u32Returns the total number of components.
const count = world.components.count();
create(ctx: *SuperComponents, comptime comp_type: type) !*ComponentCreates a new component of the specified type.
const component = try world.components.create(MyComponent);
create_c(ctx: *SuperComponents, comp_type: c_type) !*ComponentCreates a new component from a C type.
const component = try world.components.create_c(my_c_type);
expand(ctx: *SuperComponents) !voidExpands the component storage.
try world.components.expand();
gc(ctx: *SuperComponents) voidRuns garbage collection on components.
world.components.gc();
iterator(ctx: *SuperComponents) SuperComponents.IteratorReturns an iterator over all components.
var it = world.components.iterator();
while (it.next()) |component| {
// Process component
}
iteratorFilter(ctx: *SuperComponents, comptime comp_type: type) SuperComponents.MaskedIteratorReturns an iterator over components of the specified type.
var it = world.components.iteratorFilter(MyComponent);
while (it.next()) |component| {
// Process component
}
iteratorFilterRange(ctx: *SuperComponents, comptime comp_type: type, start_idx: usize, end_idx: usize) SuperComponents.MaskedRangeIteratorReturns an iterator over components of the specified type within a range.
var it = world.components.iteratorFilterRange(MyComponent, 0, 100);
while (it.next()) |component| {
// Process component
}
iteratorFilterByEntity(ctx: *SuperComponents, entity: *Entity, comptime comp_type: type) SuperComponents.MaskedEntityIteratorReturns an iterator over components of the specified type attached to an entity.
var it = world.components.iteratorFilterByEntity(entity, MyComponent);
while (it.next()) |component| {
// Process component
}
The SuperEntities struct manages all entities in the ECS.
count(ctx: *SuperEntities) u32Returns the total number of entities.
const count = world.entities.count();
create(ctx: *SuperEntities) !*EntityCreates a new entity.
const entity = try world.entities.create();
expand(ctx: *SuperEntities) !voidExpands the entity storage.
try world.entities.expand();
iterator(ctx: *SuperEntities) SuperEntities.IteratorReturns an iterator over all entities.
var it = world.entities.iterator();
while (it.next()) |entity| {
// Process entity
}
iteratorFilter(ctx: *SuperEntities, comptime comp_type: type) SuperEntities.MaskedIteratorReturns an iterator over entities with components of the specified type.
var it = world.entities.iteratorFilter(MyComponent);
while (it.next()) |entity| {
// Process entity
}
The Systems struct provides functionality for running systems in the ECS.
run(comptime f: anytype, args: anytype) !voidRuns a system function.
try Systems.run(updateSystem, .{world});
Coyote ECS provides several iterator types for iterating over entities and components.
SuperComponents.IteratorIterates over all components.
SuperComponents.MaskedIteratorIterates over components of a specific type.
SuperComponents.MaskedRangeIteratorIterates over components of a specific type within a range.
SuperComponents.MaskedEntityIteratorIterates over components of a specific type attached to an entity.
SuperEntities.IteratorIterates over all entities.
SuperEntities.MaskedIteratorIterates over entities with components of a specific type.
next(it: *Iterator) ?*ComponentReturns the next component in the iterator.
while (it.next()) |component| {
// Process component
}
Coyote ECS provides SIMD (Single Instruction, Multiple Data) optimizations for efficient processing of components.
processComponentsSimd(ctx: *_Components, comptime comp_type: type, processor: fn (*comp_type) void) voidProcesses components of the specified type using SIMD operations.
world._components[0].processComponentsSimd(MyComponent, |component| {
// Process component
});
processComponentsRangeSimd(ctx: *_Components, comptime comp_type: type, start_idx: usize, end_idx: usize, processor: fn (*comp_type) void) voidProcesses components of the specified type within a range using SIMD operations.
world._components[0].processComponentsRangeSimd(MyComponent, 0, 100, |component| {
// Process component
});
SuperComponents.MaskedIteratorIterates over components of a specific type using SIMD operations for mask checks.
var it = world.components.iteratorFilter(MyComponent);
while (it.next()) |component| {
// Process component
}
SuperComponents.MaskedRangeIteratorIterates over components of a specific type within a range using SIMD operations for mask checks.
var it = world.components.iteratorFilterRange(MyComponent, 0, 100);
while (it.next()) |component| {
// Process component
}
SuperComponents.MaskedEntityIteratorIterates over components of a specific type attached to an entity using SIMD operations for mask checks.
var it = world.components.iteratorFilterByEntity(entity, MyComponent);
while (it.next()) |component| {
// Process component
}
For more details on SIMD optimizations, see the Advanced Optimizations guide.
Coyote ECS provides a C API for cross-language compatibility.
coyote_worldA handle to a Coyote ECS world.
coyote_world world = coyote_world_create();
coyote_entityA handle to a Coyote ECS entity.
coyote_entity entity = coyote_entities_create(world);
coyote_componentA handle to a Coyote ECS component.
coyote_component component = coyote_components_create(world, type);
coyote_typeA C type definition for components.
coyote_type type = {
.id = 1,
.size = sizeof(MyComponent),
.alignof = alignof(MyComponent),
.name = "MyComponent"
};
coyote_iteratorA handle to a Coyote ECS iterator.
coyote_iterator iterator = coyote_components_iterator(world);
coyote_world_create()Creates a new Coyote ECS world.
coyote_world world = coyote_world_create();
coyote_world_destroy(world)Destroys a Coyote ECS world.
coyote_world_destroy(world);
coyote_entities_create(world)Creates a new entity.
coyote_entity entity = coyote_entities_create(world);
coyote_entities_destroy(entity)Destroys an entity.
coyote_entities_destroy(entity);
coyote_entities_count(world)Returns the number of entities.
size_t count = coyote_entities_count(world);
coyote_components_create(world, type)Creates a new component.
coyote_component component = coyote_components_create(world, type);
coyote_components_destroy(component)Destroys a component.
coyote_components_destroy(component);
coyote_components_count(world)Returns the number of components.
size_t count = coyote_components_count(world);
coyote_components_gc(world)Runs garbage collection on components.
coyote_components_gc(world);
coyote_entity_attach_component(entity, component, data, size)Attaches a component to an entity.
coyote_entity_attach_component(entity, component, &my_component_data, sizeof(MyComponent));
coyote_entity_detach_component(entity, component)Detaches a component from an entity.
coyote_entity_detach_component(entity, component);
coyote_components_iterator(world)Returns an iterator over all components.
coyote_iterator iterator = coyote_components_iterator(world);
coyote_components_iterator_next(iterator)Returns the next component in the iterator.
coyote_component component = coyote_components_iterator_next(iterator);
coyote_components_iterator_filter(world, type)Returns an iterator over components of the specified type.
coyote_iterator iterator = coyote_components_iterator_filter(world, type);
coyote_components_iterator_filter_next(iterator)Returns the next component in the filtered iterator.
coyote_component component = coyote_components_iterator_filter_next(iterator);
coyote_components_iterator_filter_range(world, type, start_idx, end_idx)Returns an iterator over components of the specified type within a range.
coyote_iterator iterator = coyote_components_iterator_filter_range(world, type, 0, 100);
coyote_components_iterator_filter_range_next(iterator)Returns the next component in the range iterator.
coyote_component component = coyote_components_iterator_filter_range_next(iterator);
coyote_entities_iterator(world)Returns an iterator over all entities.
coyote_iterator iterator = coyote_entities_iterator(world);
coyote_entities_iterator_next(iterator)Returns the next entity in the iterator.
coyote_entity entity = coyote_entities_iterator_next(iterator);
coyote_entities_iterator_filter(world, type)Returns an iterator over entities with components of the specified type.
coyote_iterator iterator = coyote_entities_iterator_filter(world, type);
coyote_entities_iterator_filter_next(iterator)Returns the next entity in the filtered iterator.
coyote_entity entity = coyote_entities_iterator_filter_next(iterator);
For more details on the C API, see the C API Guide.
Cast(comptime T: type, component: ?*Component) *TCasts a component to a specific type.
const typed_component = Cast(MyComponent, component);
CastData(comptime T: type, component: ?*anyopaque) *TCasts component data to a specific type.
const typed_data = CastData(MyComponent, component.data);
typeToId(comptime T: type) u32Converts a type to an ID.
const id = typeToId(MyComponent);
typeToIdC(comp_type: c_type) u32Converts a C type to an ID.
const id = typeToIdC(my_c_type);
opaqueDestroy(self: std.mem.Allocator, ptr: anytype, sz: usize, alignment: u8) voidDestroys an opaque pointer.
opaqueDestroy(allocator, ptr, size, alignment);