Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by gaia2222 · May 13, 2020 at 10:41 AM · savefunctionsaction

How can I add System.action to List with different parameter?

 public class tester
 {
    public List<System.Action> Skill_List= new List<System.Action>();
    void Start()
     {
        Skill_List.Add(skill1(transform,5,10f);
        Skill_List.Add(skill2(transform,target.transform);
        //I can not add the function to Skill_List
     }
     public static void skill1(Transform t, int line,float radius)
     {
        //BABABA
     }
       public static void skill2(Transform t,Transform target)
     {
       //BABABA
     }

 }

Thankyou for your reply^^

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 Bunny83 · May 13, 2020 at 11:26 AM

An expression like this:

 skill1(transform,5,10f)

would actually execute your skill1 method directly and you try to store the return value inside your list which of course doesn't make any sense.


What you need is what is called a closure. A closure is a compiler generated anonymous class which captures the variables you are using inside the closure body. What happens in the background is quite complex. Though the only important things you should remember:

  • closures are actually class instances which are generated whenever you create a closure.

  • A closure does not capture the value of a variable at the time it's created but the variable / memory location itself.


In your specific case you can do:

 Skill_List.Add(() => skill1(transform,5,10f));
 Skill_List.Add(() => skill2(transform,target.transform));


Note that the => operator is the lambda operator which essentially just creates an anonymous method. The left side specifies the parameters of your anonymous method (in our case none) while the right side defines the method's body. The lambda operator allows a very short syntax, especially for single statement / expression methods (so methods which only contain a single method call or that just evaluates to a value).


The following 3 examples are essentially equal:

 Skill_List.Add(() => skill1(transform,5,10f));
 Skill_List.Add(() => { skill1(transform,5,10f); });
 Skill_List.Add(delegate() { skill1(transform,5,10f); });





Not relevant for your question but might help in other cases: The return type of the anonymous method is usually inferred / determined from the return value of your method body. Since skill1 has no return value (void) the anonymous method also has no return value. However imagine this:

 int myMethod(int a, int b)
 {
     return a + b;
 }
 
 System.Func<int> someFunc;
 someFunc = () => myMethod(2, 3);

In this case our "someFunc" delegate is a method that doesn't take any parameters but returns an int value. The equivalent syntax for the other 2 cases would be

 someFunc = () => { return myMethod(2, 3); };
 someFunc = delegate() { return myMethod(2, 3); };

Here's another example with a method that takes a single int parameter and returns a string:

 System.Func<int, string> action;
 action = (a) => "Hello World " + a;
 action = (a) => { return "Hello World " + a; };
 action = (int a) => { return "Hello World " + a; };
 action = delegate(int a) { return "Hello World " + a; };
 action = a => "Hello World " + a;

All 5 examples are equal. Note that the last case is only possible when your lambda expression has only a single parameter. When you have more than one or zero parameters you have to use parentheses / brackets. Also note that a lambda expression can usually infer the parameter types based on what delegate type is expected. The older / explicit "delegate" syntax always requires you to specify the parameter types.


Finally I would recommend whenever you want to specify an actual method body block, use something like this:

 Skill_List.Add(() => {
     skill1(transform,5,10f);
 });

Usually you only want an explicit method body when you have more than one statement / expression. So it would become pure clutter to write everything in a single line.

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 gaia2222 · May 13, 2020 at 11:49 AM 0
Share

Wow,this worked, thanks! This exactly i need.thank!!

avatar image gaia2222 · May 13, 2020 at 12:13 PM 0
Share

Since I have some skill function have been created in static class. I want to save the parameter and integrate the function for using easily. $$anonymous$$g. When i call the function(skill), I just need to call "skill_1.invoke();" but not need to input the parameter again. The skill have been initialization in Start/Awake.

One more Question, Does the function make a copy in the List? I want to save the parameter only. What is the best way for do that?

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

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

Terrain: Use as scenario (individual) and prefab (unchanged, reusable) 0 Answers

"If there is no save data" or "If there are no PlayerPrefs" 2 Answers

save runtime created mesh class 0 Answers

PlayerPrefs and own Class 1 Answer

Player object carried from scene to scene 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