How to increase recusive call limits?

what i do is i intentionaly call the same function to figure out a path towards the grass tile

every dot stands for a tile that has been searched here i have looked through 330 tiles
and every dot is basicly equal to one recursive call

if i increase this number to 340 i get the following stack overflow error:

330 is just way to little even when i optimise my search i will still need to increase this number to at least 2000 at later stage

technically i am following Dijkstra pathfinding algorithm with some twist i dont have nodes i just use coordinates and i create a string chain the npc can walk along “www” would walk the npc 3 tiles upwards

currently i havent found a solution for this issue
so the help of a wise guru would be very welcome

how can i increase the limitation of recusrive calls allowed? 330 causes no lags or noticable delay so i am not fryn my machine

or is there a tricky way to avoid running in stack overflow, i tryed calling a new method after 300 calls which basicly continues where i left just to run in the same issue around +30 calls

Hi,

I’ve done grid based searches plenty of times. I would ask Chat GPT to show you how A* pathfinding on a grid of nodes works and convert your system to use nodes.

It really is the best way here.

Thanks,

1 Like

You’re probably allocating many new objects in memory in each recursive call that are not necessary. For example, I see that you’re using RaycastAll, that allocates a new array each time. You should substitute it with RaycastAllNonAlloc.

Worth adding that whenever you hit a problem with recursion depth, there is always a way to rewrite your code non-recursively. Typically this involves using a Stack data structure that essentially replaces the call stack. You push and pop items to and from the stack as you start and finish considering them, but you do it in an iterative loop, so you are no constrained by the call stack size. But in this specific case, A* is probably the way to go as @davidlovegrove said.

1 Like

a star is not the solution since i dont know where my goal is located so i cant use heuristics to decrease the search

i will look into that thx so far and i report the result back later

Actually now I doubt that would help as the allocated objects live in the heap anyway not the stack. My bad.

i mean i can see where my code produces arrays:
RaycastHit2D hits

if (direction == "W")
        {
            RaycastHit2D[] hits = Physics2D.RaycastAll(S + new Vector3(0, 0.6f, 0), new Vector2(0, 0.5f), 0.1f);
            //Debug.DrawRay(S + new Vector3(0, 0.6f, 0), new Vector2(0, 0.5f), Color.red, 99);
            for (int i = 0; i < hits.Length; ++i)
            {
                //print(i);
                if (hits[i].collider.gameObject.GetComponent<WalkableTile>() != null)
                {
                    //print(hits[i].collider.gameObject.name);
                    if (hits[i].collider.gameObject.GetComponent<WalkableTile>().Walkable == false)
                    {
                        tempRT = "X";
                    }
                    else if (hits[i].collider.gameObject.transform.parent.gameObject.name == "GrassAreas")
                    {
                        tempRT = "G";
                    }

                }
            }
        }

Yes but arrays are reference types and they are allocated on the heap, not the stack, so I doubt that’s what’s causing the stack overflow issue.

However on another note, I see you’re using strings to compare states and directions which is a bad idea. You should be using enums instead.

1 Like

i am using the strings for the path ^^ i cut the first letter out everytime the npc walks a step until he reaches his goal

the walking is independent from the search

Chatgpt for the win…

How to increase recursive call limits:

Edit > Project Settings > Player.
Other Settings > Mono Stack Size

he also tells me to better optimise my code then to do dis
but i am also sure i need to do both things

chatgpt tells me to look into 4 things:

  1. Replace recursion with iteration
  2. Increase stack size
  3. recursion optimization
  4. algorithm optimization