1. Hi, I need to get all building blocks in a certain distance with this code:

    (Taken from BuildingGrades)
    Code:
                List<BuildingBlock> Blocks = new List<BuildingBlock>();
                Blocks.Add(StartBlock);            Action<BuildingBlock> queue_attached_blocks = null;
                queue_attached_blocks = (Block) =>
                {
                    foreach(var collider in UnityEngine.Physics.OverlapSphere(Block.transform.position, 3.5f))
                    {
                        var next_block = collider.GetComponentInParent<BuildingBlock>();
                        if(next_block == null || Blocks.Contains(next_block)) continue;
                        queue_attached_blocks(next_block);
                    }
                };
                queue_attached_blocks(StartBlock);
    
    It works fine for small and medium sized bases.

    But as soon as I try to loop through a very large base, i get a StackOverflowException.

    Is it possible to increase the stack size somehow?
    Or would anyone be able to help me to optimize this snippet?
     
  2. Calytic

    Calytic Community Admin Community Mod

    This looks like an infinite loop. The following code should be preventing an infinite loop.

    Code:
    if(next_block == null || Blocks.Contains(next_block)) continue;
    But it's not preventing the loop because the following code is seemingly missing from inside the foreach statement..

    Code:
    Blocks.Add(next_block)
     
  3. Ah i pasted the wrong snippet, the other one is using !Blocks.Add(next_block)) continue; (HashMap)