Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tVoss opened this issue Dec 2, 2015 · 6 comments
Open

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

tVoss opened this issue Dec 2, 2015 · 6 comments

Comments

@tVoss
Copy link

tVoss commented Dec 2, 2015

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
Copy link
Owner

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
Copy link
Author

tVoss commented Dec 2, 2015

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
Copy link
Author

tVoss commented Dec 2, 2015

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

tVoss commented Dec 2, 2015

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants