Giter Club home page Giter Club logo

ecs's Introduction

ECS

This is a simple C++ header-only type-safe entity component system library. It makes heavy use of C++11 constructs, so make sure you have an up to date compiler. It isn't meant to do absolutely everything, so please feel free to modify it when using. There's a VS2015 solution provided, but it should compile on any standard compiler with C++11 support (C++14 is more ideal, however, as it lets you use auto parameters in lambdas).

Again, this is meant for quick prototyping or as a starting point for a more advanced ECS toolkit. It works and it works well, but it isn't optimized for speed or anything.

This has been tested on the following compilers:

  • Visual Studio 2015 on Windows 10 (x64)
  • G++ 5.4.1 (using -std=c++11 and -std=c++14) on Ubuntu 14.04 (x64)

Contributions are welcome! Submit a pull request, or if you find a problem (or have a feature request) make a new issue!

Tutorial

This ECS library is based on the Evolve Your Hierarchy article. If you haven't read it, please do or else things won't make much sense. This is a data-driven entity component system library, and to know how to work with it you need to know what that entails (this is not the same as Unity's components so don't expect it to be).

Your first components

Components in ECS can be any data type, but generally they'll be a struct containing some plain old data. For now, let's define two components:

struct Position
{
    Position(float x, float y) : x(x), y(y) {}
    Position() : x(0.f), y(0.f) {}

    float x;
    float y;
}

struct Rotation
{
    Rotation(float angle) : angle(angle) {}
    Rotation() : angle(0) {}
    float angle;
}

This isn't the most realistic example - normally you'd just have a single transform component for a game but this should help illustrate some functionality later. Also note that we don't have to do anything special for these structs to act as components, though there is the requirement for at least a default constructor.

Create a system

Now we need some logic to act on that data. Let's make a simple gravity system:

class GravitySystem : public EntitySystem
{
public:
    GravitySystem(float amount)
    {
        gravityAmount = amount;
    }
    
    virtual ~GravitySystem() {}
    
    virtual void tick(World* world, float deltaTime) override
    {
        world->each<Position>([&](Entity* ent, ComponentHandle<Position> position) {
            position->y += gravityAmount * deltaTime;
        });
    }
    
private:
    float gravityAmount;
}

This is a pretty standard class definition. We subclass EntitySystem and implement the tick() method. The world provides the each method, which takes a list of component types and runs a given function (in this case a lambda) on every entity that has those components. Note that the lambda is passed a ComponentHandle, and not the component itself.

Alternate iteration methods

In addition to the lambda-based each, there's also an iterator-based each, made to be used with the range based for loop. Lambda-based each isn't a true loop, and as such you can't break from it. Instead, you can use a range based for loop. The downside is that it will not directly expose components as arguments, but you can combine it with Entity::with for a similar result:

for (Entity* ent : world->each<Position>())
{
    ent->with<Position>([&](ComponentHandle<Position> position) {
	    position->y += gravityAmount * deltaTime;
	});
}

Alternatively, you may retrieve a single component at a time with Entity::get, though this will return an invalid component handle (see ComponentHandle<T>::isValid and ComponentHandle<T>::operator bool()) if there isn't a component of that type attached:

ComponentHandle<Position> position = ent->get<Position>();
position->y += gravityAmount * deltaTime; // this will crash if there is no position component on the entity

with<T>() only runs the given function if the entity has the listed components. It also returns true if all components were found, or false if not all components were on the entity.

Finally, if you want to run a function on all entities, regardless of components, then use the all function in the same way as each:

world->all([](Entity* ent) {
	// do something with ent
});

You may also use all in a range based for loop in a similar fashion to each.

Create the world

Next, inside a main() function somewhere, you can add the following code to create the world, setup the system, and create an entity:

World* world = World::createWorld();
world->registerSystem(new GravitySystem(-9.8f));

Entity* ent = world->create();
ent->assign<Position>(0.f, 0.f); // assign() takes arguments and passes them to the constructor
ent->assign<Rotation>(35.f);

Now you can call the tick function on the world in order to tick all systems that have been registered with the world:

world->tick(deltaTime);

Once you are done with the world, make sure to destroy it (this will also deallocate the world).

world->destroyWorld();

Custom Allocators

You may use any standards-compliant custom allocator. The world handles all allocations and deallocations for entities and components.

In order to use a custom allocator, define ECS_ALLOCATOR_TYPE before including ECS.h:

#define ECS_ALLOCATOR_TYPE MyAllocator<Entity>
#include "ECS.h"

Allocators must have a default constructor. When creating the world with World::createWorld, you may pass in your custom allocator if you need to initialize it first. Additionally, custom allocators must be rebindable via std::allocator_traits.

The default implementation uses std::allocator<Entity>. Note that the world will rebind allocators for different types.

Working with components

You may retrieve a component handle (for example, to print out the position of your entity) with get:

ComponentHandle<Position> pos = ent->get<Position>();
std::cout << "My position is " << pos->x << ", " << pos->y << std::endl;

If an entity doesn't have a component and you try to retrieve that type from it, get will return an invalid component handle:

ComponentHandle<Position> pos = otherEnt->get<Position>(); // assume otherEnt doesn't have a Position component
pos.isValid(); // returns false, note the . instead of the ->

Alternatively, you may use a handle's bool conversion operator instead of isValid:

if (pos)
{
    // pos is valid
}
else
{
    // pos is not valid
}

Events

For communication between systems (and with other objects outside of ECS) there is an event system. Events can be any type of object, and you can subscribe to specific types of events by subclassing EventSubscriber and calling subscribe on the world:

struct MyEvent
{
    int foo;
    float bar;
}

class MyEventSubscriber : public EventSubscriber<MyEvent>
{
public:
    virtual ~MyEventSubscriber() {}
    
    virtual void receive(World* world, const MyEvent& event) override
    {
        std::cout << "MyEvent was emitted!" << std::endl;
    }
}

// ...

MyEventSubscriber* mySubscriber = new MyEventSubscriber();
world->subscribe<MyEvent>(mySubscriber);

Then, to emit an event:

world->emit<MyEvent>({ 123, 45.67f }); // you can use initializer syntax if you want, this sets foo = 123 and bar = 45.67f

Make sure you call unsubscribe or unsubscribeAll on your subscriber before deleting it, or else emitting the event may cause a crash or other undesired behavior.

Systems and events

Often, your event subscribers will also be systems. Systems have configure and unconfigure functions that are called when they are added to/removed from the world and which you may use to subscribe and unsubscribe from events:

class MySystem : public EntitySystem, public EventSubscriber<MyEvent>
{
    // ...
    
    virtual void configure(World* world) override
    {
        world->subscribe<MyEvent>(this);
    }
    
    virtual void unconfigure(World* world) override
    {
        world->unsubscribeAll(this);
        // You may also unsubscribe from specific events with world->unsubscribe<MyEvent>(this), but
        // when unconfigure is called you usually want to unsubscribe from all events.
    }
    
    // ...
}

Built-in events

There are a handful of built-in events. Here is the list:

  • OnEntityCreated - called when an entity has been created.
  • OnEntityDestroyed - called when an entity is being destroyed (including when a world is beind deleted).
  • OnComponentAssigned - called when a component is assigned to an entity. This might mean the component is new to the entity, or there's just a new assignment of the component to that entity overwriting an old one.
  • OnComponentRemoved - called when a component is removed from an entity. This happens upon manual removal (via Entity::remove() and Entity::removeAll()) or upon entity destruction (which can also happen as a result of the world being destroyed).

Avoiding RTTI

If you wish to avoid using RTTI for any reason, you may define the ECS_NO_RTTI macro before including ECS.h. When doing so, you must also add a couple of macros to your component and event types:

// in a header somewhere
struct MyComponent
{
	ECS_DECLARE_TYPE; // add this at the top of the structure, make sure to include the semicolon!

	// ...
};

// in a cpp somewhere
ECS_DEFINE_TYPE(MyComponent);

Again, make sure you do this with events as well.

Additionally, you will have to put the following in a cpp file:

#include "ECS.h"
ECS_TYPE_IMPLEMENTATION;

If you have any templated events, you may do the following:

template<typename T>
struct MyEvent
{
	ECS_DECLARE_TYPE;

	T someField;

	// ...
}

template<typename T>
ECS_DEFINE_TYPE(MyEvent<T>);

ecs's People

Contributors

abeimler avatar afpatmin avatar machinemitch21 avatar marcbritton avatar plyul avatar redxdev avatar zfyu222 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ecs's Issues

Not an issue, just a question

Awesome library. Works perfectly. I just have a question about general use. Should there be 1 global world or one in every scene? I have written a scene manager and i cant find an answer anywhere regarding this. I am leaning towards one for every scene..... but id love to hear how you use it. Thanks.

Sorry i know this is the wrong place to ask questions but i dont understand github.

Plans to add Multi thread?

Hi!

First of all , you have done a great job developing this library.

Has any plan to addd Multi thread support to this library?

Regards!

What about supporting singleton components

I'm using your library to write a simple game. Your library is wonderful and it helps me a lot. But I hope that you could add APIs to support to create singleton components (came from GDC2017 'Overwatch Gameplay Architecture and Netcode', I'm sorry that I didn't find the video), which is like global variables that directly belong to the world instead of entities. I think they are helpful when you need components with camera position, window width and height, time, etc, that need to share between systems but does not logically belong to any entity.

I add some codes to complement a simple version, using a singleton entity to store the singleton components, but it's not gentle at all:

class World
{
public:
	Entity* singletons;

	template<typename T, typename... Args>
	void createSingletonComponent(Args&&... args) {
		this->singletons->assign<T>(args...);
	}

	template<typename T>
	ComponentHandle<T> getSingletonComponent() {
		return this->singletons->get<T>();
	}
};

I hope you could write these APIs in your way.

Can't build with cmake and g++ 9.2.0

I try to build ECS with my other Project (didn't work), so I fork ESC and add my typical cmake-file and compiler-options (try out C++14 and C++17): https://github.com/abeimler/ECS

OS: Arch Linux x86_64 (4.16.81-1-MANJARO)
Compiler: c++ (GCC) 9.2.0
CMake: 3.15.5

Build:

mkdir build
cmake . -Bbuild -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DECS_SAMPLE_BUILD:BOOL="1"
cmake --build build

Error:

[ 50%] Building CXX object CMakeFiles/sample.dir/sample.cpp.o
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/ext/new_allocator.h:146:8: error: allocation of incomplete type 'ECS::Entity' [clang-diagnostic-error]
                            _Up(std::forward<_Args>(__args)...)))
                            ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/alloc_traits.h:483:24: note: in instantiation of exception specification for
 'construct<ECS::Entity, ECS::World *, unsigned long &>' requested here
        noexcept(noexcept(__a.construct(__p, std::forward<_Args>(__args)...)))
                              ^
ECS.h:454:4: note: in instantiation of exception specification for 'construct<ECS::Entity, ECS::World *, unsigned long &>
' requested here
                        std::allocator_traits<EntityAllocator>::construct(entAlloc, ent, this, lastEntityId);
                        ^
ECS.h:131:8: note: forward declaration of 'ECS::Entity'
        class Entity;
              ^
1 error generated.
Error while processing sample.cpp.
Found compiler errors, but -fix-errors was not specified.
Fixes have NOT been applied.

Found compiler error(s).
make[3]: *** [CMakeFiles/sample.dir/build.make:63: CMakeFiles/sample.dir/sample.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:105: CMakeFiles/sample.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:117: CMakeFiles/sample.dir/rule] Error 2
make: *** [Makefile:131: sample] Error 2

Option to disable a system

I'd like to disable certain systems from running based on certain game events. To support would require being able to get a system from the world and call a method or set a field to disable it. Systems that are disabled would not have their tick methods called until reenabled.

World::each causes Internal compiler error if class has an implementation file

This header file compiles and works perfectly:

#pragma once

#include "ECS.h"

struct TestComp {
	int x;
	int y;
};

class TestClass {
public:
	TestClass() {}
	virtual ~TestClass() = default;

	void update(ECS::World* world, float dt) {
		using namespace ECS;
		world->each<TestComp>([&](Entity* ent, ComponentHandle<TestComp> testComp) {
			testComp->x += 1;
		});

	}
};

however, as soon as an implementation file is present, for example TestClass.cpp, even if it just contains

#include "TestClass.h"

Visual Studio shows an internal compiler error (C1001) at the last line of the implementation file. This error remains whatever i do, using inline, moving the implementation to the .cpp file and so on.

Im running the latest Visual Studio enterprise 2017, v. 15.5.7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.