Last active
August 29, 2015 14:00
-
-
Save dustinfreeman/11048767 to your computer and use it in GitHub Desktop.
Expecting to catch entity destruction events, but not getting them. Added a global entity in case destroy() has to be called in the middle of an update() function for the event to be caught.
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> | |
bool destruction_caught = false; | |
entityx::Entity testEntity_global; | |
struct TestEvent : public entityx::Event<TestEvent> { | |
TestEvent(std::string text) : text(text) {} | |
std::string text; | |
}; | |
struct TestSystem : public entityx::System<TestSystem>, entityx::Receiver<TestSystem> { | |
void configure(entityx::EventManager &event_manager) { | |
//subscribe to entity destruction | |
event_manager.subscribe<entityx::EntityDestroyedEvent>(*this); | |
event_manager.subscribe<TestEvent>(*this); | |
} | |
void receive(const entityx::EntityDestroyedEvent &entity_destruction) { | |
std::cout << "entity destroyed \n"; | |
destruction_caught = true; | |
} | |
void receive(const TestEvent &test_event) { | |
std::cout << "Test Event: " << test_event.text << "\n"; | |
} | |
void update(entityx::EntityManager &es, entityx::EventManager &events, double dt) override { | |
std::cout << "system update \n"; | |
events.emit<TestEvent>("this is a test event!"); | |
//destroy the global entity in the update() functon | |
//should also trigger an entity destruction event | |
if (testEntity_global.id() != entityx::Entity::INVALID) | |
testEntity_global.destroy(); | |
} | |
}; | |
class GameManager : public entityx::EntityX { | |
public: | |
void configure() { | |
std::cout << "configured \n"; | |
systems.add<TestSystem>(); | |
systems.configure(); | |
} | |
void initialize() { | |
std::cout << "initialized \n"; | |
} | |
entityx::Entity create_entity() { | |
return entities.create(); | |
} | |
void update(double dt) { | |
systems.update<TestSystem>(dt); | |
} | |
}; | |
int main(int argc, const char * argv[]) | |
{ | |
GameManager* entity_manager = new GameManager(); | |
entity_manager->configure(); //is this redundant? | |
entity_manager->initialize(); | |
//create a test entity. | |
entityx::Entity testEntity = entity_manager->create_entity(); | |
testEntity_global = entity_manager->create_entity(); | |
entity_manager->update(1); | |
testEntity.destroy(); // should trigger an entity destruction event | |
entity_manager->update(1); | |
if (destruction_caught) | |
std::cout << "PASS! entity destruction caught. \n"; | |
else | |
std::cout << "FAIL! entity destruction NOT caught. \n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code snippet should catch an event when testEntity is destroyed. However, it does not.