• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

80 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

Related Questions

A node in a childnode? 1 Answer

Handling multiple tags 1 Answer

How to make a list of Actions or methods? 1 Answer

How can i use 4.6 delegate lists in my scripts ? 1 Answer

Getting Parameters of a Constructor 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges