• 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
Question by Omti1990 · Oct 02, 2018 at 07:54 PM · functionsdelegates

How to feed several (0-n) functions into another function

Hello, I've got a question about how I can feed several functions (with arguments) into another function. I assume this has to work via delegates somehow, but I'm not sure how.

The basic idea is I want to have an unmark function that I can feed other functions such as "Remove buildings" or "build buildings" or just null (in case that I don't actually want to do anything than to remove all my markings).

Currently it looks like this:

 if (tool == Tool.Remove)
         {
             for (int x = xMin; x <= xMax; x++)
             {
                 for (int z = zMin; z <= zMax; z++)
                 {
                     Mark(x, z, 0);
                     Destroy(gameObjects[x, markingOld.y, z]);
                     fieldInfos[x, markingOld.y, z].construction = Construction.Empty;
                 }
             }
         }


What I want to do is to feed the

 Destroy(gameObjects[x, markingOld.y, z]);
 fieldInfos[x, markingOld.y, z].construction = Construction.Empty;

into a function like this:

 private void UnmarkAll(Delegate function)
     {
         for (int x = xMin; x <= xMax; x++)
         {
             for (int z = zMin; z <= zMax; z++)
             {
                 Mark(x, z, 0);
                 //Execute everything I fed into the argument
                 function();
             }
         }
     }

So I don't need to spam the for loops everywhere and keep my code somewhat compact.

I'd also like the option to feed a null into the "UnmarkAll" function. Or just one function. Is there a way to do this?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by TreyH · Oct 02, 2018 at 08:13 PM

If you're comfortable with a fairly unsightly syntax, you can use a lambda function cast as System.Action:

edit: added arguments, didn't see that you needed them.

 private void UnmarkAll(System.Action<int, int> action)
 {
     if (tool == Tool.Remove)
     {
         for (int x = xMin; x <= xMax; x++)
         {
             for (int z = zMin; z <= zMax; z++)
             {
                 Mark(x, z, 0);
                 action(x, z);
             }
         }
     }
 }


You'd then call it like this:

 this.UnmarkAll((x, z)=>{
     Destroy(gameObjects[x, markingOld.y, z]);
     fieldInfos[x, markingOld.y, z].construction = Construction.Empty;
 });
Comment
Omti1990

People who like this

1 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 Omti1990 · Oct 03, 2018 at 10:13 AM 1
Share

Thank you very much. This works perfectly.

I just don't understand how. From the way the syntax looks the (x,z) is assigned to the functions. But what actually seems to happen is that the functions are fed into the UnmarkAll function and are assigned the arguments x and z from the UnmarkAll function.

These lambdas confuse me...

Actually does the (x, z) actually matter when the function is called or could I put some other variable name in there? Like:

 this.UnmarkAll((var1, var2)=>{
      Destroy(gameObjects[var1, markingOld.y, var2]);
      fieldInfos[var1, markingOld.y, var2].construction = Construction.Empty;
  });

Instead of

 this.UnmarkAll((x, z)=>{
      Destroy(gameObjects[x, markingOld.y, z]);
      fieldInfos[x, markingOld.y, z].construction = Construction.Empty;
  });

And it'd still work? I don't think I really understand what this is actually doing.

avatar image TreyH Omti1990 · Oct 03, 2018 at 12:27 PM 1
Share

Try it out for yourself! These are also called "Anonymous Functions" as you don't give them a name, if that's less esoteric than "Lambda".

The names do not matter when you actually make the lambda function itself because the compiler already knows what variable types to expect (from the UnmarkAll definition). In that definition, we told C# that it was going to receive an Action (which can be a named or anonymous function that returns nothing) using two integer arguments. So when we actually call UnmarkAll, it is assuming that your lambda arguments are integers.

It might seem closer to python than a strongly typed language like C#, but types are being enforced here. :-)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

92 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 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 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 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 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 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

How to inject dependancies written in C# into Unityscript? 1 Answer

Using Master Function to Call Abilities 3 Answers

How to get the subscribed functions of a multicast delegate? 1 Answer

Get Object, Trigger Attached Method,Trigger method on collider script 1 Answer

I'm struggling how to fix this error. Not all paths return a value. Any Help appreciated 2 Answers


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