EntityComponentSystem
Small implementation of c++ entity component system inspired by Unity
Install / Use
/learn @lukaschod/EntityComponentSystemREADME
EntityComponentSystem
About
This is small implementation of entity component system with C++. The API is heavily inspired by Unity ECS framework, but it does not share any underling implementation. This project is solely done for fun, to see how much it is possible to recreate Unity like ECS framework in C++ with similar API.
Requiraments
Project was create with Visual Studio 2019 so it is recommended to open with it. However it is only few files, as result it should be trivial to move to any prefer IDEA.
Features
Systems that are used for executing game logic. In most cases ues entity queries for modifying data.
class MySystem : public System
{
public:
using System::System;
virtual void OnCreate() {}
virtual void OnUpdate() {}
};
Component type that can be any non generic struct.
struct MyComponent
{
MyComponent(float value) { Value = value; }
float Value;
};
Entity Manager for handling all operations related with entities.
virtual void OnCreate()
{
EntityArchetype archetype = Manager.CreateArchetype({ typeof(MyComponent) });
Entity entity = Manager.CreateEntity(archetype);
Manager.SetComponentData(entity, MyComponent(1));
Manager.AddComponentData(entity, MyComponent2(2));
printf("%f \n", Manager.GetComponentData<MyComponent2>(entity).Value);
Manager.DestroyEntity(entity);
}
Entity queries for fetching entity component data and executing in for each loop.
virtual void OnUpdate()
{
Entities().ForEach(
[](Entity entity, cread(MyComponent) myComponent)
{
printf("Entity:(%d, %d) MyComponent:%f\n", entity.Index, entity.Version, myComponent.Value);
}).Run();
Entities().ForEach(
[](Entity entity, cwrite(MyComponent) myComponent)
{
myComponent.Value = 5;
}).Run();
}
Creating world that allows easier way to create systems and execute them.
World world; // Create world
world.GetOrCreateSystem<MySystem>(); // Create system
world.Update(); // Execute single frame update
Other features without example:
- Jobs.
- Serialization.
- Profiling.
- Command Buffer.
Limitations
- Chunk resizing is not supported.
- ForEach works with maximum 3 arguments. However can easily be extending in
NodeVision.Entities.hpp. - Dynamic buffer is not supported.
Example Single Threaded
void MinimalDemo()
{
struct MyComponent
{
MyComponent(float value) { Value = value; }
float Value;
};
class MySystem : public System
{
public:
using System::System;
virtual void OnCreate()
{
EntityArchetype archetype = Manager.CreateArchetype({ typeof(MyComponent) });
Entity entity = Manager.CreateEntity(archetype);
Manager.SetComponentData(entity, MyComponent(1));
}
virtual void OnUpdate()
{
Entities().ForEach(
[](Entity entity, cread(MyComponent) myComponent)
{
printf("Entity:(%d, %d) MyComponent:%f\n", entity.Index, entity.Version, myComponent.Value);
}).Run();
}
};
World world; // Create world
world.GetOrCreateSystem<MySystem>(); // Create system
world.Update(); // Execute single frame update
}
Output:
Entity:(0, 0) MyComponent:1.000000
Example Multi Threaded
void MinimalDemo()
{
struct MyComponent
{
MyComponent(float value) { Value = value; }
float Value;
};
class MySystem : public System
{
public:
using System::System;
virtual void OnCreate()
{
EntityArchetype archetype = Manager.CreateArchetype({ typeof(MyComponent) });
Entity entity = Manager.CreateEntity(archetype);
Manager.SetComponentData(entity, MyComponent(1));
}
virtual void OnUpdate()
{
Entities().ForEach(
[](Entity entity, cread(MyComponent) myComponent)
{
printf("Entity:(%d, %d) MyComponent:%f\n", entity.Index, entity.Version, myComponent.Value);
}).Schedule();
}
};
WorkerManager workerManager; // Create multi threads
workerManager.Start({ WorkerContext() });
World world(&workerManager); // Create world
world.GetOrCreateSystem<MySystem>(); // Create system
world.Update(); // Execute single frame update
workerManager.Stop();
}
Output:
Entity:(0, 0) MyComponent:1.000000
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
