- Home /
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)
{
}
}
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.
@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
Follow this Question
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