C# newbie here.
In one of my classes (an Entity class, to be precise), I have a delegate that takes in an Entity and another related class:
public delegate void FiringFunc(Entity e, BulletFactory fact)
and a loop in the Entity class that calls this function every frame (if its defined):
FiringFunc firingFunc = null; //defined later
if(firingFunc)
firingFunc(this, someBulletFactory);
As one could probably tell, this is a delegate that serves as a bullet firing function (you would code something like a timer for the bullet, the angles to fire at, etc). However, a thought occurred to me: what if I wanted the bullet to have a slight difference, but still remain the same (something like a tad bit slower, a slightly different color, in a different direction, etc). I would have to create another function to serve as the delegate – this seemed wrong to me.
Here is an example of what creating and setting the delegate would look like:
Entity e = new Entity( ... )
e.firingFunc = FiringFunctions.SomeFiringFunctionName;
Is there a way I could add parameters to this? It would be great if I could do something akin to the following:
e.firingFunc = FiringFunctions.SomeFiringFunctionName(someChange1, someChange2);
Try
This creates a new anonymous function (the lambda) that calls
FiringFunctions.SomeFiringFunctionNamewith the included parameters.This assumes that FiringFunctions.SomeFiringFunctionName is defined as: