Reputation: 103
This question pertains to an XNA 4.0 game so it would naturally have a simple game execution loop:
Init()
Load()
Update()
Draw()
etc.
I am hoping to make this loop as clean as possible, I've been planning it in my head and I want to write down how I want things done before actually integrating it. How practical would delegates be as a kind of method queue?
Init()
initQueue(RequiredArgs)
Load()
loadQueue(RequiredArgs)
etc.
I would add methods to the queue as needed, might make multiple delegates so I don't have to add 100 args to my delegates and execute multiple methods together that share similar arguments.
Upvotes: 1
Views: 1131
Reputation: 203835
There's nothing wrong with the general idea of a collection (often a queue) of delegates. It's a reasonable implementation of a message loop.
It's worth noting that if messages can be added to the queue from multiple threads it may make sense to use BlockingCollection
as the collection for the queue so that the multithreaded access to the collection isn't a problem.
Specifically the type of the collection can just be BlockingCollection<Action>
. There's no need to manage allowing any other delegate signatures. There's nothing for you to do with any output, and you won't have any parameters to give it. If someone wishes to call a method that has parameters they can curry the function to fix the parameters (possibly using a lambda, such as () => someFunction(param1, param2);
).
Upvotes: 3
Reputation: 17544
From what I understand, you have a few options.
First, make a List<Action>
, or some other list that holds your delegates. This is so you can foreach your way through the delegates.
As for the parameters, you can:
a) have it take a List
, Array
, or params
as an input (these would be of type object). This allows a weekly-defined input of unlimited values. Your functions would just have to know which is which, and hope to god that you inserted the correct ones. I would not recommend this unless you can't do...
b) make a base abstract class which serves as the Base Settings
for all delegate function calls. Then make specialized inherited classes for each delegate that needs specific things. This might be more time consuming, but would be worth it in the long run (for maintenance and readability).
Upvotes: 2