Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Overcast · Oct 01, 2013 at 03:02 AM · c#delegates

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.

  1. Is this a stupid way to do what I am trying to achieve?

  2. 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
 }
 
 

 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Oct 01, 2013 at 05:23 PM 1
Share

Alternatively, just have the function declaration be

 public void AddRequirement(Requirement method)
avatar image Overcast · Oct 03, 2013 at 11:58 AM 1
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges