richardbiely / gaia-ecs

A simple and powerful entity component system (ECS) written in C++17
MIT License
77 stars 3 forks source link

Runtime types - ability to define components in runtime (useful mostly for scripting) #13

Open richardbiely opened 10 months ago

richardbiely commented 9 months ago

Technically exists already. We can now define run-time tags because components are defined as entities now.

ecs::World w;
ecs::Entity teamA = w.add(); // entity representing some teamA we made up at run-time
ecs::Entity teamB = w.add(); // entity representing some teamB we made up at run-time
ecs::Entity player1 = w.add();
ecs::Entity player2 = w.add();
ecs::Entity player3 = w.add();
w.add(player1, teamA); // player1 now belongs to teamA
w.add(player2, teamA); // player2 now belongs to teamA
w.add(player3, teamB); // player3 now belongs to teamB

For values, we'd have something like this:

struct IntVal { int value; };
struct FltVal { float value; };
GAIA_ID(IntVal) = w.add<IntVal>();
...
ecs::Entity health = w.add();
ecs::Entity currVal = w.add();
ecs::Entity maxVal = w.add();
w.add(currVal, GAIA_ID(IntVal));
w.add(maxVal, GAIA_ID(IntVal));
w.add(health, currVal, IntVal{ 10 });
w.add(health, maxVal, IntVal{ 100});
w.add(player, health);