On Thursday, 1 December 2016 at 23:51:19 UTC, Payotz wrote:
So, to give context, I am trying to make an event manager for a
game I'm making.
I was writing the "register()" method so I ran into a problem.
The register method will take in delegates as an argument, but
those delegates have varied arguments themselves, so I can't
really put anything there. I know that it's got something to do
with templates so I tried my hand in it and came up with this:
void registerEvent(string event_name,T...)(T delegate() dg);
You could do what pthread does in C to achieve similar goals.
void delegate(void*); // or int delegate(void*) if you want
to return an error code
and then store the void pointer along with the delegate
and then cast the void* to the correct type (a struct of
arguments or similar) and call it later. not very safe but there
are ways to make it nicer.
However if the set of types of events is closed you can use
ailas event = Algebraic!(ev1,ev2,ev3...);
where ev1 and so on are structs that hold parameters of the event.
This is effectively how SDL does events.
I know there's something missing in the way I did it, so I'll
be glad for you folks to tell me what I'm missing.
And for the second part of the question, I can't seem to make a
Dynamic Array where I could store the delegates taken in the
"registerEvent" method. Closest thing I have gotten to is this:
private void delegate(T)(T args)[string] event;
which resulted in the compiler screaming Error signs at me.
So how does one do it?
You get an error here because you are trying to have an
associative array of templates. if they were all void
delegate(int)'s that would work. You can't use an uninstantiated
template as a type.