I'm doing my first event system in C++ using fastdelegates(codeproject) and although its already working, I'm thinking of doing a cleaner design.
Well, the basic problem when using delegates is that it is necessary to specify the handler's parameter types. So first I started using a Variant class based on a union, but later switched to simple pointers cause I couldn't use custom constructors.
I ended up with something like:
Code: Select all
eventManager->defineEvent("explosion");
Person *person = new Person();
eventManager->bindEvent("explosion", EventManager::Delegate2(person, &Person::OnExplosion));
eventManager->fireEvent("explosion", Variant(Vector3(1,2,3)), Variant(0.5f));
void Person::OnExplosion(Variant* v1, Variant* v2){
v1->getVector3Ptr();
//...
Then I started thinking maybe I could use a map to pair names with types, but I can't figure out a way to do it.
I would like to have:
Code: Select all
Event event("explosion");
event.setProperty("position", Vector3()); //The second parameter could be a Variant
event.setProperty("force", int(10));
eventManager->fireEvent("explosion", event);
void Person::OnExplosion(Event* e){
Vector3 v = e->getProperty("position"); // How to do this?
float f = e->getProperty("force"); // Same function returns a Vector3 and a float?
//...