r-lyeh-archived / kult

:crystal_ball: Lightweight entity/component/system library (C++11)
zlib License
128 stars 5 forks source link

kult::entities() always returns 0 entities #2

Open tVoss opened 8 years ago

tVoss commented 8 years ago

Under the current implementation of kult::entities(), nothing is ever returned. My current method for getting all entities (and deleting them) now is looping through all the component types and purging them that way. Hopefully a solution to this can be found.

r-lyeh-archived commented 8 years ago

Are you sure about this? Can you compile tests.cxx and check output for last two tests? This is what I get:

[ OK ] 617 entities().size() == 2 (No error)
[ OK ] 620 entities().size() == 0 (No error)
tVoss commented 8 years ago

Ah because for your test the entities are always in scope. In my use case I'll create the entity in one method, and then try to access all of them in a different method. This causes no live instances to actually exist, just reference to ids in the respective component classes.

tVoss commented 8 years ago

Since everything kult related is accessed in a static manner, and my EntityManager is very object oriented, my solution is adding a manager component to each entity created which holds the id of the EntityManager that created it. This allows me to easily get all related entities that I need with kult::join and an if statement around the id.

r-lyeh-archived commented 8 years ago

Or you could have something like this instead:

class EntityManager {
std::vector<kult::entity*> entities;
kult::entity &add();
void del( const kult::entity &);
}

kult::entity &EntityManager::add() {
entities.push_back( new kult::entity );
return *entities.back();
}

void EntityManager::del( const kult::entity &k ) {
entities.erase( std::find(k, entities.begin(), entities.end() );
}
r-lyeh-archived commented 8 years ago

You dont need N factories to create N type of entites. A single factory should handle them all.

Example to create different kind of entities with a single factory:

EntityManager em;
kult::entity &ent1 = em.add();
kult::entity &ent2 = em.add();
ent1[ position ] = vec2f();
ent2[ shape ] = circle();
assert( kult::entities() == 2 );
em.del( ent1 );
em.del( ent2 );
assert( kult::entities() == 0 );
tVoss commented 8 years ago

This method of manually adding and removing entities takes away a lot of magic that made your library great! Take a look at my project to see how I'm using the manager component to get all the entities that belong to a certain EntityManager.

https://github.com/tVoss/AnotherGame

Sorry for being the only one create issues in this repo!