Skip to content

Instantly share code, notes, and snippets.

View SanderMertens's full-sized avatar
🐥
Feeding flecs

Sander Mertens SanderMertens

🐥
Feeding flecs
View GitHub Profile
@SanderMertens
SanderMertens / archetype_groups.c
Last active March 2, 2019 07:42
An idea for adding groups to an archetype-based ECS
enum JoinKind {
InnerJoin,
OuterJoin
};
/** Create group.
* A group stores a set of entities for a given list of archetypes.
* Entities within the group will be ordered according to a component
* pivot list, such that the first component is guaranteed to be
typedef struct Radar {
float range;
float speed;
} Radar;
void SetRadar(ecs_rows_t *rows) {
ECS_COLUMN(rows, Radar, radar, 1);
ECS_COLUMN_COMPONENT(rows, EcsCircle, 2);
ECS_COLUMN_COMPONENT(rows, EcsAngularMomentum2D, 3);
int main(int argc, char *argv[]) {
ecs_world_t *world = ecs_init_w_args(argc, argv);
ECS_IMPORT(world, FlecsComponentsTransform, ECS_2D);
ECS_IMPORT(world, FlecsComponentsGeometry, ECS_2D);
ECS_IMPORT(world, FlecsComponentsGraphics, ECS_2D);
ECS_IMPORT(world, FlecsComponentsInput, ECS_2D);
ECS_IMPORT(world, FlecsSystemsSdl2, ECS_2D);
ECS_ENTITY(world, WheelPrefab, EcsPrefab, EcsCircle, EcsColor);
#include <meta_test.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
ECS_STRUCT(Position, {
float x;
float y;
/* -- Create single entity -- */
FLECS_EXPORT
ecs_entity_t ecs_new_w_entity(
ecs_world_t *world,
ecs_entity_t component);
FLECS_EXPORT
ecs_entity_t ecs_new_w_type(
ecs_world_t *world,

Creating entities


Entity creation, empty (n = 1000000):

Framework Measurement
EnTT 0.003885
EnTT 0.002851 (batching)
struct BulletCount {
int32_t count;
};
struct Damage {
float value;
};
int main(int argc, char * argv[]) {
flecs::world world(argc, argv);

Here are some generic design guidelines (emphasis on guidelines, not rules):

  • When building a new feature, start with the components and spend time thinking about how the components will be used (how often are they written/read, who owns them, how many instances etc)

  • Design components so that they have a single purpose. It is much easier (and cheap) to combine two components in a query than it is to split up components, which causes a lot of refactoring.

  • Don't over-generalize. If your code has lots of branches it is possible that you're trying to do too much with a single component. Consider splitting up components. It's ok to have multiple components with the same fields, if this makes your business logic simpler (and faster).

  • Think twice before adding collections to components. Collections are OK if the contents of the collection are meant to be interpreted as an atomic unit, but if the individual elements must be interpreted in a standalone way, consider creating separate entities instead.

World


World creation (n = 10):

Framework Measurement
flecs 0.005501 World creation
info: bootstrap
info: | threading available
info: | time management available
info: | bootstrap core components
info: | | table [EcsComponent,6,CHILDOF|280] created
info: | | table [EcsComponent] created
info: | | table [Type] created
info: | | table [Name] created
info: | | table [CHILDOF|280] created
info: | | table [Name,CHILDOF|280] created