- Home /
Recursive Function
Hi all,
I'm trying to generate a level and calling for this a GenerateRoom() function. In this function I am again calling GenerateRoom(), and so forth.
My problem is that after the function was called it doesn't return to the position where it was called to continue the code after the function GenerateRoom().
This results that my level only populates always to the right. In a crossroom the other two directions are ungenerated.
I think I have some logic flaws with the localIterationDepth or just don't know how to programm it proper recursive.
Well, I'm a bit stuck hopefully somebody can help
void GenerateRoom( GameObject startObject, int localIterationCounter ) {
if( iterationCounter > maxIterationDepth ) {
//no more global iterations
} else {
localIterationCounter++;
Transform[] myTempAnchorPoints = new Transform[5];
List<Transform> myTrueTempAnchorPoints = new List<Transform>();
//get all children
myTempAnchorPoints = startObject.GetComponentsInChildren<Transform>();
//get all anchorpoints
for( int tempCounter = 0; tempCounter < myTempAnchorPoints.Length; tempCounter++ ) {
if( myTempAnchorPoints[tempCounter].tag == "AnchorPoint" ) {
myTrueTempAnchorPoints.Add( myTempAnchorPoints[tempCounter] );
myTempAnchorPoints[tempCounter].tag = "AnchorPointUsed";
}
}
Debug.Log(myTrueTempAnchorPoints.Count);
//create rooms at anchorpoints, if some are available
if( myTrueTempAnchorPoints.Count == 0 ){
return;
} else {
for( int tempCounter = 0; tempCounter <= myTrueTempAnchorPoints.Count; tempCounter++ ) {
Quaternion rotation = new Quaternion();
rotation = myTrueTempAnchorPoints[tempCounter].rotation;
rotation.eulerAngles += new Vector3(0f, 180f, 0f);
myTempGameObject = Instantiate( levelObjects[GetRoom()], myTrueTempAnchorPoints[tempCounter].position, rotation ) as GameObject;
if( localIterationCounter < maxLocalIterationDepth ) {
GenerateRoom( myTempGameObject, localIterationCounter ) ;
} else {
localIterationCounter = 0;
iterationCounter++;
return;
}
}
}
}
}
Comment