kult icon indicating copy to clipboard operation
kult copied to clipboard

kult::entities() always returns 0 entities

Open tVoss opened this issue 9 years ago • 6 comments

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.

tVoss avatar Dec 02 '15 19:12 tVoss

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)

r-lyeh-archived avatar Dec 02 '15 21:12 r-lyeh-archived

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 avatar Dec 02 '15 21:12 tVoss

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.

tVoss avatar Dec 02 '15 21:12 tVoss

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 avatar Dec 02 '15 21:12 r-lyeh-archived

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 );

r-lyeh-archived avatar Dec 02 '15 21:12 r-lyeh-archived

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!

tVoss avatar Dec 02 '15 21:12 tVoss