Skip to content

Instantly share code, notes, and snippets.

@dustinfreeman
Last active December 29, 2015 15:58
Show Gist options
  • Save dustinfreeman/7693920 to your computer and use it in GitHub Desktop.
Save dustinfreeman/7693920 to your computer and use it in GitHub Desktop.
A test component and system for EntityX (https://github.com/alecthomas/entityx). Does not seem to iterate through any entities from the entities_with_components call. Am I doing something wrong?
#include <iostream>
#include <string>
#include <vector>
#include <entityx/entityx.h>
std::vector<entityx::Entity> entities_vec;
struct TestComponent : entityx::Component<TestComponent> {
TestComponent(std::string _name = "test") {
name = _name;
}
std::string name;
};
struct TestSystem : public entityx::System<TestSystem> {
void update(entityx::ptr<entityx::EntityManager> es, entityx::ptr<entityx::EventManager> events, double dt) override {
//entityx::EntityManager::View entities = es->entities_with_components<TestComponent>();
entityx::ptr<TestComponent> test;
for (auto entity : entities_vec) {
//entityx::ptr<TestComponent> test = entity.component<TestComponent>();
entity.unpack<TestComponent>(test);
if (test) { //null test
std::cout << "test: " << test->name << "\n";
}
}
std::cout << "=====================\n";
}
};
entityx::ptr<entityx::EventManager> events(new entityx::EventManager());
entityx::ptr<entityx::EntityManager> entities(new entityx::EntityManager(events));
class GameManager : public entityx::Manager {
public:
void configure() {
system_manager->add<TestSystem>();
system_manager->configure();
}
void initialize() {
//create any starting entities here.
//create a test entity.
entityx::Entity testEntity = entities->create();
testEntity.assign<TestComponent>("test entity from initialize.");
entities_vec.push_back(testEntity);
}
void update(double dt) {
//call all the subsystems here, in order.
system_manager->update<TestSystem>(dt);
}
};
int main(int argc, const char * argv[])
{
GameManager* entity_manager = new GameManager();
entity_manager->configure(); //not sure when this is to be called.
entity_manager->initialize(); //looks like we have to call this ourselves. Not called by the constructor.
//create a test entity.
entityx::Entity testEntity = entities->create();
testEntity.assign<TestComponent>("test entity from main.");
entities_vec.push_back(testEntity);
// insert code here...
std::cout << "Hello, World!\n";
float loop_time_s = 1;
while(true) {
sleep(loop_time_s);
entity_manager->update(loop_time_s);
std::cout << "looped.\n";
}
return 0;
}
//Current output shows no entities printed:
//GOT:
/*
Hello, World!
=====================
looped.
=====================
looped.
=====================
looped.
*/
//EXPECTED:
/*
Hello, World!
test: test entity from initialize.
test: test entity from main.
=====================
looped.
test: test entity from initialize.
test: test entity from main.
=====================
looped.
test: test entity from initialize.
test: test entity from main.
=====================
looped.
*/
@dustinfreeman
Copy link
Author

I have "fixed" my issues by separately storing the entities in a std::vector. However, I was under the impression that entities->create() would add the new entity to an internal list in EntityX, which would be queried by es->entities_with_components< ... >(). Is this correct?

@dustinfreeman
Copy link
Author

See Rev 1 for my usage of entity querying originally. And, as far as I can tell, should work according to the EntityX examples given.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment