0

I'm absolutely lost at trying to port the Richard Lord's Ash Framework to "pure" C++ (MSVC Express 2008) or at least i hasnt been able to find any implementation alike, I'm messing with the boost:fusion libs to implement the minimun reflection requirements that the frameworks require but template metaprograming comes as new for me and I'm overflow with compiler errors and time-consuming failure testing :( ...

Anyone knows about the feasibility or usefulness to port the framework?

Taking the Entity class as example, could someone clarify how could i achieve a result like :

(THIS IS FLEX CODE!)

    waitEntity = new Entity( "wait" )
            .add( new WaitForStart( waitView ) )
            .add( new Display( waitView ) )
            .add( new Position( 0, 0, 0 ) );

with an C++ implementaton like this?

    ...
    namespace bf = boost::fusion;
    namespace bm = boost::mpl;

    template<typename ComponentMap>
    class EntityASH{
    public:
    EntityASH(ComponentMap c)
    :m_components(c){   }
    //"fusion-style" trying to return a new Components template entity for a new component type
    template<typename T>
    EntityASH
    template<typename T>
/*¿Here im absoltely lost
            EntityASH<
            result_of::as_map<
                    typename result_of::push_back<ComponentMap, 
                    fusion::pair<T,T>
                    >::type
                    >
            >*/
    EntityASH *add(){
            typedef bf::pair<T, T> newTentryPair;
            //bf::as_map(bf::push_back<ComponentMap,newTentryPair>(m_components,newTentryPair()));

            return new EntityASH(bf::as_map(bf::push_back<ComponentMap,newTentryPair>(m_components,newTentryPair())));

...

1 Answers1

0

A Constructor in C++ cannot return something, therefore you have to use another method to create the entity whereby createEntity() shall return a reference or a std::shared_ptr of the entity object.

I see no advantage in using templates here because storing references to the components seems to be more correct to me because components are parts of the entitys and do not belong to the entity:

std::shared_ptr<Entity> createEntity() {
    return std::make_shared<Entity>();
}

...

std::shared_ptr<Entity> addComponent(ComponentBase comp) {
    mComponents.add(comp);
    return shared_from_this();
}

...

createEntity().add(new Position())
          .add(new Display())
          .add(new Position());

I do not get why you want to use templates here? Do you want the components to become a real part of the entity type? That would mean that you cannot add/remove components during runtime in an easy way.

I think in most ECS the entity is only a container that is there for usability reasons and can entirely replaced by a simple integer ID but because that destroys the usability entities store the references to their components for easier resolution for the programmer.

DarthB
  • 351
  • 2
  • 6
  • Now i understand your point better, as i said in my post, i'm totally blackout by trying to implement this framework. I googled the FLex tutorial as a base to mimic on C++, but it was no easy task. Right now im struggling with the serialization/Reflection part and see no other way to implement it than writing a parser using boost:spirit to achieve the same result... – user2996040 Feb 11 '14 at 17:02
  • Maybe data-oriented design is a good keyword for you. Serialization is really simple when using data-oriented principles. For reflection you may use std::type_index and/or std::type_info and the type_id operator. – DarthB Feb 14 '14 at 19:56