I am trying to implement a calendar with callbacks for a game. In other words, a calendar that has a current date that increments as time passes (the date is incremented in each update() call). I have already implemented a date class and it works fine. The calendar includes notions of tasks, each of which has a date. When the date arrives, all tasks assigned to that date are performed.
Currently, I am implementing the tasks as a sorted dictionary with the Date as the key (or, really, a float that is the conversion of a date object into a float number of days) and a List<> of custom objects called a “CalendarTask” as the value. CalendarTask has a custom delegate called Task which accepts one parameter – object o. CalendarTask also has arguments for that custom delegate – the object o. The reason the dictionary has a List<> of CalendarTasks is that there can be more than one task scheduled for a particular date and each has to be called when the date arrives.
The way that the Calendar executes the tasks is that every update() call, the calendar checks to see whether the current date is greater than the First() key of the sorted dictionary. If greater, Calendar fetches the List of CalendarTasks corresponding to the First() key and executes all CalendarTasks in the List.
My question is am I over-complicating this? In other words, I have this hunch that I can just use events in the sorted dictionary. This would simplify the registration of CalendarTasks to the dictionary, as I could simply += a method. And it would simplify the execution of tasks on a particular date as I could simply invoke the event once.
The problem with using events is that I cannot store the parameter to pass when the event gets called in the event itself. I’d need to create a custom class/object anyway. Also, each method registered to the event would need to somehow “know” which stored argument corresponds to itself. In other words, I couldn’t just invoke the event once with a single parameter, I’d have to call it multiple times for each stored argument.
Am I doing something ridiculous here? Am I missing something simple?
I’m wondering, can I use an anonymous function to store the arguments? Specifically, when I register the task to the calendar, instead of registering a particular function (say task()) and arguments (say args), I can register an anonymous function whose body is just task(args). Then the calendar would not need to explicitly store the args. Does that make sense?
Thanks!