0

How do I store function inside a function in a List<>?

I know its a bit confusing but idk how to explain it in more details. That's how I understand my problem.

So I wanna store functions inside a List<> and one of the functions consists another functions inside it. Since the List is executed as first in first out, when it comes to the functions with another functions, I want it to execute the child until it finishes before it continues to another element in the parent List.

For scenario, the method with another method inside is A Loop. I want it to iterate until certain condition is met then it stops. Then, continues to another method store in parents List.

As for now, I can store method inside List. It just idk how to execute the method inside method.

Here is the sneak peak of the code :/

private List<Function_> TranslateCodeFromBlocks(Transform parent, List<Function_> sequence_, List<Function_> insideLoop)
{
    foreach (Transform child in parent)
    {
        var functionName = child.name.Split('_');

        if (functionName[0] == "Function")
        {
            string function = functionName[1];
            switch (function)
            {

                case "MoveRight":
                    sequence_.Add(new MoveRight("MoveRight"));

                    break;
                case "MoveLeft":
                    sequence_.Add(new MoveLeft("MoveLeft"));
                    break;
                case "MoveUp":
                    sequence_.Add(new MoveUp("MoveUp"));
                    
                    break;
                case "MoveDown":
                    sequence_.Add(new MoveDown("MoveDown"));
                    break;

                case "MoveLoop":

                    foreach (Transform compLoop in parent)
                    {
                        var loopName = child.name.Split('_');

                        if (loopName[0] == "Function")
                        {
                            string tempLoop = loopName[1];
                            switch (function)
                            {
                                case "MoveRight":
                                    insideLoop.Add(new MoveRight("MoveRight"));
                                    break;
                                case "MoveLeft":
                                    insideLoop.Add(new MoveLeft("MoveLeft"));
                                    break;
                                case "MoveUp":
                                    insideLoop.Add(new MoveUp("MoveUp"));
                                    break;
                                case "MoveDown":
                                    insideLoop.Add(new MoveDown("MoveDown"));
                                    break;
                            }
                        }
                    }
                    sequence_.Add(new MoveLoop(insideLoop)); //this return error. Idk what to code, im stucked here.
                    break;

            }
        }
    }

    return sequence_;
}

}

It's a bit complicated, I hope you guys understand it. so as you can see, when it comes to MoveLoop (which consists few game object), I want the code to find each of the child and add it into another List<> which called insideLoop. This List<> will execute just normal, it's just happen to be inside of another functions called MoveLoop. The rest beside MoveLoop, is added into the parent List<> called sequence.

public MainLoop(Rigidbody2D mainTarget, List<Function_> sequence_)
{
    this.end = false;
    this.stop = false;
    this.mainTarget = mainTarget;
    this.sequence_ = sequence_;

}
public IEnumerator Play()
{
    int i;


    foreach (Function_ fun in this.sequence_)
    {
        fun.Func(this.mainTarget);
        this.stop = true;
        Stop();

        yield return new WaitForSeconds(1);

    }


}

}

And then this code is to execute the List<> called Sequence which is the parent List<>. How do I call and execute the List<> insideLoop while List<> Sequence_ is being executed?

public class MoveUp : Function_ {
public MoveUp(string ID) : base(ID)
{
    this.ID = ID;
}

override public void Func(Rigidbody2D mainTarget)
{
    Vector2 position = mainTarget.position;
    position.y += 50;
    mainTarget.MovePosition(position);
    moveUp = true;


}

public class MoveRight : Function_ {
public MoveRight(string ID) : base(ID)
{
    this.ID = ID;
}

override public void Func(Rigidbody2D mainTarget)
{
    Vector2 position = mainTarget.position;
    moveRight = true;
    position.x += 45;
    mainTarget.MovePosition(position);

}

Here are few sneak peak of what the MoveUp, MoveRight do.

Im new to this so pardon me if my programming terms are wrong.

EDIT: Problem Solved.

  • hi and elcome. Please post the code of "the method consists another method inside it" – Mong Zhu Dec 01 '22 at 16:37
  • I don't understand your question or problem, and by the sounds of it you're probably approaching your actual problem, incorrectly. But if you still want to store methods in a list you could use List of Func. – FernandoG Dec 01 '22 at 16:38
  • Ive edited my question. Will u please take a look @MongZhu – blossom rosé Dec 01 '22 at 17:12

1 Answers1

1

You can store delegates within a list.

List<Func<int, int>> funcs = new List<Func<int, int>>();

funcs.Add(i => i * 2);

funcs[0].Invoke(1); //returns 2

Please, take a look at Func, Action and Predicate delegates, see Delegates: Predicate vs. Action vs. Func .

nestor10
  • 474
  • 4
  • 12