Understanding the SpawnUntilFull() function

Hello i watched this video for 3 times and i still don’t understand it.
Firstly in

Transform NextFreePosition()

function what does in actualy do it checks if there is no child objects than it returns one child object ok i think i undestand it but i dont understand function > void spawnUntilFull() if someone can help me with understanding of this function
thanks

void SpawnUntilFull()
{
	Transform freePosition = NextFreePosition ();
	
	if (freePosition) 
	{
		GameObject enemy = Instantiate (enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
		enemy.transform.parent = freePosition;
	}
	
	if(NextFreePosition())
	{
		Invoke ("SpawnUntilFull", spawnDelay);
	}
}


Transform NextFreePosition()
{
	foreach (Transform childPositionGameObject in transform)
	{
		if (childPositionGameObject.childCount == 0) 
		{
			return childPositionGameObject;
		}
	}
	
	return null;
}
2 Likes

Hi @Andraz_Cuderman,

The short version is basically, you have a series of available positions which can hold an enemy. If all of the positions have an enemy already, you don’t spawn anymore. But if there are still spaces in the formation, you create a new enemy in the available space. You keep doing this until all of the spaces are full.


To explain;

Transform NextFreePosition()
{
	foreach (Transform childPositionGameObject in transform)
	{
		if (childPositionGameObject.childCount == 0) 
		{
			return childPositionGameObject;
		}
	}
	
	return null;
}

This method offers a return type of Transform (e.g. the Transform component of a Game Object).

We can see that the method has two return points. In the first scenario, a Transform will be returned. If this return doesn’t happen, the code will return on the null.

So, you know now that you will either get a Transform or a null value back by return of this method.

In the case of the first instance, the foreach statement iterates through all of the child GameObjects of the current GameObject (e.g. the child Transforms of the Transform).

It then tests to see if that Transform has a child count of zero. If it does that tells us that it does not yet have an enemy, so, we return the Transform which has no children (so we can put an enemy in it).


void SpawnUntilFull()
{
	Transform freePosition = NextFreePosition ();
	
	if (freePosition) 
	{
		GameObject enemy = Instantiate (enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
		enemy.transform.parent = freePosition;
	}
	
	if(NextFreePosition())
	{
		Invoke ("SpawnUntilFull", spawnDelay);
	}
}

This method tries to create a new enemy in a free position, if one exists.

Firstly it tries to obtain a Transform from the above method (NextFreePosition()). Remember, this will either return the Transform which can have an enemy put in it or, null.

The statement, if (freePosition) checks to see if freePosition is true, if it is then it cannot be null. This indicates that a valid Transform was returned in which we can put an enemy.

So, within that if statement, we then create a new enemy as a copy of a enemy prefab and set it’s parent transform, to the Transform which was returned as available. The enemy will now be in the “position” that was free.

The second if statement calls the same method again, but with a slight delay. This is recursion, something you have to be very careful with as it’s a great way to consume all available memory.

In this case the spawnDelay value is used to delay the next call of the same method, at which point it will run through all of the same steps again. Note, it has called NextFreePosition() again here to do this, so only if there is another free position will it call itself (SpawnUntilFull()).

It is a little bit wasteful as if there is a free position, when the same method is called again it calls the NextFreePosition() method again to then get the actual Transform.


I hope the above explanation makes some sense, if not, ask me some specifics on what was not clear.

I also reformatted the code in your post to make it more readable.


See also;

5 Likes

Privacy & Terms