Home > Blockchain >  Means to count how many times a recursion was called or how to identify if all recursions were done
Means to count how many times a recursion was called or how to identify if all recursions were done

Time:08-24

I'm in need of means to check if how many times the recursive part of this function was activated to check when the first call was fulfilled, it's part of the designed true game over that needs the first call to be fulfilled without achieving all objectives.

    IEnumerator CompositeRun(string name)
    {
        BotOperation operation;
        compositeOps.TryGetValue (name, out operation);
        CompositeOperation compOp = operation as CompositeOperation;
        List<BotOperation> botOpList = compOp.opList;

        if (botOpList != null)
        {
            for (int i = 0; i < botOpList.Count; i  )
            {
                BotOperation op = botOpList [i];
                CompositeOperation comp = op as CompositeOperation;

                if (comp != null)
                {
                    // If this operation is a composite type, then we need to execute it first accordingly.
                    yield return StartCoroutine (CompositeRun(comp.name));
                }
                if (op != null && op.ValidateOperation(gameObject, levelDef))
                {
                    op.RunOperation (gameObject, levelDef);

                    if (CheckGameOver ())
                    {
                        // If all objectives are fulfilled, then there's no need to continue running the processes, the stage is concluded.
                        StopAllCoroutines ();
                    }
                }
                
                yield return new WaitForSeconds (operationDelay);
            }
        }
        yield return null;
    }

CodePudding user response:

I suggest starting with passing the recursion depth value

But are you 100% sure you cannot resolve this without recursion? Recursion seems like a heavy cannon to kill a fly here

IEnumerator CompositeRun(string label, uint recursionDepth = 0)
{
    compositeOps.TryGetValue (label, out var operation);
    var compOp = operation as CompositeOperation;
    var botOpList = compOp.opList;
    if (botOpList != null)
    {
        var delay = new WaitForSeconds (operationDelay);
        for (int i = 0; i < botOpList.Count; i  )
        {
            var op = botOpList[i];
            var comp = op as CompositeOperation;

            if (comp != null)
            {
                // If this operation is a composite type, then we need to execute it first accordingly.
                yield return StartCoroutine (CompositeRun(comp.name,recursionDepth 1));
                Debug.Log($"we need to go deeper @ depth:{recursionDepth 1}");
            }

            if ( op?.ValidateOperation(gameObject, levelDef))
            {
                op.RunOperation (gameObject, levelDef);

                if (CheckGameOver ())
                {
                    // If all objectives are fulfilled, then there's no need to continue running the processes, the stage is concluded.
                    StopAllCoroutines ();
                }
            }
            
            yield return delay;
        }
    }
    yield return null;
}
  • Related