-
-
Save dustinfreeman/7725413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 testEntity1 = entity_manager->create(); | |
//create a component, and assign it to this entity | |
entityx::ptr<TestComponent> testComponent1 = testEntity1.assign<TestComponent>("test component from assign"); | |
//create a test entity using syntax as given in EntityX README | |
entityx::Entity testEntity2 = entity_manager->create(); | |
entityx::ptr<TestComponent> testComponent2 = new TestComponent("test component from new"); | |
//ERROR GIVEN BY ABOVE LINE: No viable conversion from 'TestComponent *' to 'entityx::ptr<TestComponent>' (aka 'std::__1::shared_ptr<TestComponent>') | |
testEntity2.assign(testComponent2); | |
} | |
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