I have the following code where i try to recursively call a function:
if( value == 1 ) function();
I tested and the value is not always 1 but i get a StackOverflow error when running.
I can't seem to find the problem.
I could use some help solving this.
Thanks in advance.
Edit:
private void AddNeighbors( Vector2 o, Vector2 s )
{
//Debug.Log( "Called" + o );
int notAdded = 0;
// LEFT //
int[ , ] left = GetRoom();
int leftZeroCount = 0;
for( int x = 0; x < left.GetLength( 0 ); ++x )
{
float a = o.x - left.GetLength( 0 ) + x;
for( int y = 0; y < left.GetLength( 1 ); ++y )
{
float b = o.y + y;
if( a > 0 && b > 0 && grid[ ( int )a, ( int )b ] == 0 )
++leftZeroCount;
}
}
if( leftZeroCount == left.GetLength( 0 ) * left.GetLength( 1 ) )
{
int add = Random.Range( 0, 2 );
if( add == 1 )
{
Vector2 origin = new Vector2( o.x - left.GetLength( 0 ), o.y );
for( int x = 0; x < left.GetLength( 0 ); ++x )
{
float a = origin.x + x;
for( int y = 0; y < left.GetLength( 1 ); ++y )
{
float b = origin.y + y;
grid[ ( int )a, ( int )b ] = left[ x, y ];
}
}
AddNeighbors(
origin,
new Vector2( left.GetLength( 0 ), left.GetLength( 1 ) )
);
}
else ++notAdded;
}
else ++notAdded;
// CHECK IF ADDED //
if( notAdded == 1 ) AddNeighbors( o, s );
}
The GetRoom function returns an random int[ , ] composed of ones and zeros.
↧