- Home /
Help filtering objects using delegates, passing methods to methods for delegate instancing
Hi Everyone,
I'm not actually 100% sure I need to use delegates here - I am still learning c# and can't achieve what I want to do as of yet.
I have a (non mb)class which is designed to hold a reference to a GameObject, set by the user at runtime via a GUI.
When the user assigns an object from the field to this class, I want to optionally filter out certain world objects from the selection options, based on a variety of different logic evaluations.
I had thought to do this, I should set up a number of methods within the class, and then specify which methods have to validate in order for the object to be within the selection criteria.
Do I have set up a delegate:
public delegate bool Requirement(GameObject g);
And then have a number of methods which return bool and require GameObject.
I then want to build a list of delegate instances which must pass in order for the world objects to pass the filter.
So I can do this:
Requirement r = RequireActive;
requirements.Add(r);
Where RequireActive is one such method .
But I then wanted to set up a method to effectively do this, and I can't get the method to work. Currently it looks like this:
public void AddRequirement(Func<GameObject, bool> method){
Requirement r = method;
requirements.Add(r);
}
Which gives me the following message:
Can't implicitly convert System.Func' to `Requirement'
So, my question is two-fold.
Is this a stupid way to do what I am trying to achieve?
How do I pass a function to another method, in such a way it can still be used to create a delegate instance?
Thanks for your time, I apologise for the long winded question.
Answer by AndyMartin458 · Oct 01, 2013 at 05:08 PM
I think your idea is pretty good. A list of requirements is a good way to go about this. I believe your problem is how you're creating the Requirement. You are making a reference variable, so you need to use the new keyword. Here is what I would suggest.
public delegate bool Requirement(GameObject g);
List<Requirement> requirements = new List<Requirement>();
public void AddRequirement(Func<Gameobject, bool> method){
//Need to create this with new
Requirement r = new Requirement(method);
requirements.add(r);
}
Alternatively, just have the function declaration be
public void AddRequirement(Requirement method)
@Andy$$anonymous$$artin458 Thanks for the reply, and your comment about my implementation, good to know I haven't completely missed the point.
I ended up using Jamora's suggestion because it pointed out the parameter I needed to remove my current compile error. Seems obvious now!
As for you original suggestion, thanks for pointing out the alternate:
new Requirement(method);
previous examples I had seen simply instantiate them like this:
// Instantiate the delegate.
Del handler = Delegate$$anonymous$$ethod;
@Jamora, thanks for your advice. Aside from my question about my actual application of delegates, this was the bit I was really trying to solve.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to unsubscribe from InputSystem event properly 0 Answers
Sort Items Based on distance to Camera 1 Answer
System or Unity Action? 1 Answer