Skip to content

Instantly share code, notes, and snippets.

@alecthomas
Forked from dustinfreeman/gist:7693920
Last active December 29, 2015 18:08
Show Gist options
  • Save alecthomas/7708478 to your computer and use it in GitHub Desktop.
Save alecthomas/7708478 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <entityx/entityx.h>
struct TestComponent : entityx::Component<TestComponent> {
explicit 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) {
// entityx::ptr<TestComponent> test = entity.component<TestComponent>();
entity.unpack<TestComponent>(test);
if (test) { //null test
std::cout << "test: " << test->name << "\n";
}
}
std::cout << "=====================\n";
}
};
class GameManager : public entityx::Manager {
public:
void configure() {
system_manager->add<TestSystem>();
}
void initialize() {
//create any starting entities here.
//create a test entity.
entityx::Entity testEntity = entity_manager->create();
testEntity.assign<TestComponent>("test entity from initialize.");
}
void update(double dt) {
//call all the subsystems here, in order.
system_manager->update<TestSystem>(dt);
}
};
int main(int argc, const char * argv[])
{
GameManager* game = new GameManager();
game->start();
// insert code here...
std::cout << "Hello, World!\n";
float loop_time_s = 1;
while(true) {
sleep(loop_time_s);
game->update(loop_time_s);
std::cout << "looped.\n";
}
game->stop();
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.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment