Skip to content

Instantly share code, notes, and snippets.

@SuperV1234 SuperV1234/example_lua_ecs.cpp Secret
Created Jan 13, 2016

Embed
What would you like to do?
// Original code
Entity* createGhostEntity()
{
Entity* e = new Entity;
// create a graphics component
GraphicsComponent* gc = new GraphicsComponent;
gc->setFilename("ghost.png");
e->addComponent(std::type_index(typeid(GraphicsComponent)), gc);
// create an NpcComponent
NpcComponent* npcc = new NpcComponent;
npcc->setPhrase("I'M A SCARY GHOST!!!");
e->addComponent(std::type_index(typeid(NpcComponent)), npcc);
return e;
}
// ---
// Code I would write:
class context
{
public:
auto create_entity()
{
// allocate and construct an entity
}
template<typename T, typename... Ts>
auto create_component(Ts&&... xs)
{
// allocate and construct a component
// by perfect-forwarding `xs...`
}
};
auto create_ghost_entity(context& ctx)
{
auto e(ctx.create_entity());
auto gc(ctx.create_component<graphics_component>("ghost.png");
e.attach<graphics_component>(gc);
auto npcc(ctx.create_component<npc_component>("I'M A SCARY GHOST!!!");
e.attach<npc_component>(npcc);
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.