Skip to content

Instantly share code, notes, and snippets.

@dustinfreeman
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinfreeman/11048767 to your computer and use it in GitHub Desktop.
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.
#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;
}
@dustinfreeman
Copy link
Author

This code snippet should catch an event when testEntity is destroyed. However, it does not.

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