ECS back and forth

Hello, Habr! I present to you the translation of the article "ECS back and forth - Part 1 - Introduction" by Michele skypjack Caini .


ECS back and forth


Part 1 - Introduction.


When I first learned about the architectural template entity entity component system, I went to look for more information about it on the Internet. But, unfortunately, then enough light was not shed on this topic, and there were no resources where different approaches with their pros and cons would be described. Almost every article, post, comments (a significant proportion of them) were about one specific implementation and only slightly referred to other examples.


In this post I will try to get you up to speed and discover some introductory models for you, giving some tips so that you can make your own ECS.


Why should I use ECS?


Try not to be fooled by what they say around. If you are working on AAA projects at a serious level, the main reason why you should use such a serious tool is the organization of the code, and not (only) the performance. Of course, performance is not the last thing, but a well-organized code base is priceless, and with most games you will not have performance problems, be they written using the OPP paradigm or with another optional implementation of the component template.


In essence, component-oriented programming is an extremely powerful tool that allows you to make code easily extensible and speed up the development cycle. Undoubtedly, all this should be your primary goal.


, . , , .



, .


, . () . , , , , . : .
, . , , , .


, , , . , , .


?


( ), update . , , , .


, . . , , . , .


, , - .


ECS back and forth


ECS.


, , .
«Game Programing Patterns». -.


, ECS , .


, , .



- . , , . , . , .


– . .


. , — . , .


, , , , -. - , , . , - . , , , - .


family generator. , . , : entityx and EnTT.


family generator.


class family {
    static std::size_t identifier() noexcept {
        static std::size_t value = 0;
        return value++;
    } 

public:
    template<typename>
    static std::size_t type() noexcept {
        static const std::size_t value = identifier();
        return value;
    }
};

:


const auto id = family::type<my_type>();

. . , , .


family generator . , , , .


:


- ( ) . – ECS, , , - .


, , - , .


?


, , . , , . bitset .


, , . family generator , . .


, — . , , , . :


void my_nth_system(std::vector<game_objects> &objects) {
    for(auto &&object: objects) {
        if(object.has<position, velocity>()) {
            auto &pos = object.get<position>();
            auto &vel = object.get<velocity>();

            // ...
        }
    }
}

, : , . , , .


. , . , : , , .


. , , , . , , , , , . , . , , .


, – . ? . , . , ? , - , .


: —


– .


. , . . , . .


, , - , — :). , . 0, – 1, . , , 0, 1.


? , , , 0 0, 1 1, . , N M , .


. , , . , . - , entityx, ECS C++.


bitset . . , C N > 0. C , N . C 0, . ? Bitsets – .


, , , bitsets .


, , . , , , , , -, . , : - - , , . , , , , , . bitset , .


?


, . , , , , , .


, . , , , , .


,


, , .


, , GitHub , . , .


.


All Articles