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
0
Question by baribal · Feb 08, 2018 at 01:01 PM · listparametersdelegatemethodssequence

Sequence of methods stored in the list with parameters

Hi,

I'm trying to make a sequence of actions stored in a list to iterate through it to execute the sequence. I used delegates. I made it work without parameters. But I want to pass to them one ore more parameters ( int, method etc ). What to do to make this work? How to store methods and parameters together?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Example : MonoBehaviour
 {
     private delegate void ActionSequence(int time);
 
     private List<ActionSequence> AttackSequence = new List<ActionSequence> ();
 
 
     void Start()
     {
 
         AttackSequence.Add (ActionSequenceHandler(moveToFront, 1));
         AttackSequence.Add (ActionSequenceHandler(moveToEnemy, 2));
         AttackSequence.Add (ActionSequenceHandler(moveToOrigin, 1));
 
         // AttackSequence[0](); 
 
         // even more params
         //AttackSequence.Add (ActionSequenceHandler(moveToOrigin, 1, method));
 
     }
 
     private void ActionSequenceHandler(ActionSequence theAction, int time)
     {
         theAction(time);
     }
 
     private void moveToFront(int time)
     {
         print (time);
     }
 
     private void moveToEnemy(int time)
     {
         
     }
 
     private void moveToOrigin(int time)
     {
         
     }
 }
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 · Feb 08, 2018 at 01:48 PM

A lot in your code doesn't make much sense and wouldn't compile. Your "ActionSequenceHandler" is a method that doesn't return anything since it's return type is void. However when you

 ActionSequenceHandler(moveToFront, 1)

you actually call that method. In turn your method will simply invoke the moveToFront method. Since the ActionSequenceHandler doesn't return anything you can not "add" the result to your list.


If you want to store a method reference as well as it's parameter in a list to be able to call it at a later point in time you have to use a class instance that holds the method as well as the parameter. One way is to use closures. Closures are actually compiler generated classes to represent a method reference including a closure context of variables.

Your ActionSequence delegate represents a method reference to a method that expects an int parameter. That int parameter is expected when you want to call that method reference. So your "AttackSequence" list should be of type List<System.Action>.

 private List<System.Action> AttackSequence = new List<System.Action>();

You can add closures without any parameters to that list:

      AttackSequence.Add ( ()=>moveToFront(1) );
      AttackSequence.Add ( ()=>moveToEnemy(2) );
      AttackSequence.Add ( ()=>moveToOrigin(1) );

So when doing AttackSequence[1](); you will actually call moveToEnemy(2).


Internally the compiler will generate a closure class with methods like those:

 private class SomeInternalName
 {
     public static void Closure001()
     {
         moveToFront(1);
     }
     public static void Closure002()
     {
         moveToEnemy(2);
     }
     public static void Closure003()
     {
         moveToOrigin(1)
     }
 }
     // [ ... ]
     AttackSequence.Add ( SomeInternalName.Closure001 );
     AttackSequence.Add ( SomeInternalName.Closure002 );
     AttackSequence.Add ( SomeInternalName.Closure003 );


Though it's not clear how you want to use this AttackSequence list. Normal methods can't "wait" for a certain time as it has to complete within the same frame. You can use coroutines instead. Coroutines are already implicit closure object.

Comment
Add comment · Show 1 · 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 baribal · Feb 08, 2018 at 03:12 PM 0
Share

@Bunny83 I thought I would add theAction and parameters into Dictionary in ActionSequenceHandler or something like that. I don't know if it even would work. But thanks to your reply now I have what I wanted in simple way. And now I know what Closures are.

AttackSequence list will be iterated by the control method which will be fired at the end of the tween animations in each of these methods in the sequence. ( moveToFront, moveToEnemy, ... etc ).

thank you

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

77 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

Related Questions

A node in a childnode? 1 Answer

Getting Parameters of a Constructor 0 Answers

Sorting a list by distance to an object? 1 Answer

Set up method parameter in Editor ? 2 Answers

How to pass a parameter only if certain condition is true? 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