Hey guys!
I have a simple event-system implemented in my game. To trigger an event I have to create an instance of that specific event; But in order to avoid creating many of them in a short time I wanted to somehow pool them: The only problem is that I don't want to create a pool for each of them, and I also have to pass different variables for each event as constructor arguments. Here is an example showing what I would like to do:
Superclass GameEvent.h:
class GameEvent {}
Poolable
template<typename T> class Poolable {
public:
virtual void reset() = 0;
virtual T* new(...) = 0;
}
ExampleEvent1
class ExampleEvent1 : public GameEvent, public Poolable<ExampleEvent1> {
private:
ExampleEvent1(SomeArg1 a1, SomeArg2 a2);
public:
EventSpecificFunction();
void reset(){//reset this object to default}
ExampleEvent1* new(...){
//do the same stuff you would do in the constructor
return this;}
friend EventHandler;
}
ExampleEvent2
class ExampleEvent2 : public GameEvent, public Poolable<ExampleEvent2> {
private:
ExampleEvent2(Arg a1, Arg a2, int id, std::string test);
public:
EventSpecificFunctionLalalala();
//poolable functions like above...
friend EventHandler;
}
EventHandler
class EventHandler {
public:
void triggerEvent(EventType type, GameEvent e); //EventType is an enum
void hookInto(EventType type, fastdelegate::FastDelegate1<GameEvent, void> fun);
~EventHandler();
EventHandler();
void recycleEvent(EventType type, GameEvent* e){
e->reset();
eventPool.at(type).push(e);
}
GameEvent* newEvent(EventType type, I_Guess_I_Will_Have_To_Use_varargs){
if(eventPool.at(type).size() < 1)
return new //somehow construct an event according to the EventType type with valid arguments...
return eventPool.at(type).pop()->new(I_Guess_I_Will_Have_To_Use_varargs);
}
private:
std::map<EventType, A3D::AEvent<FastDelegate<void(GameEvent)>>> gameEvents;
std::map<EventType, queue<GameEvent*>> eventPool;
};
I hope this isn't too messy. My problem lies therein that I can't create a new instance of one of my events by only knowing the according value from the enum. Also I don't like varargs, but that's the smallest of my problems.
Help would be greatly appreciated .
Kind regards
Edit: Wait, couldn't I just make newEvent a templated function? *mindblown*