Sequence of methods stored in the list with parameters


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)

	private void moveToFront(int time)
		print (time);

	private void moveToEnemy(int time)

	private void moveToOrigin(int time)

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()
    public static void Closure002()
    public static void Closure003()
    // [ ... ]
    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.