[SOLVED] 4.27 Actors spawning on parts of grid that they shouldn't

EDIT
I’m the biggest dummy in the world. Look at the code:

if (TileToSpawn = GrassTile) {

I didn’t put double equals. :man_facepalming:


Followed a tutorial on how to create a grid out of hexagons, and it worked smashingly. Then I wanted to spawn trees on some of the tiles.

Quick note, there are two tiles [Grass, Water] and I only want the trees to spawn on the grass. I figured when spawning the tiles, I could just check if the code was going to spawn a grass tile or a water tile, and if it was grass then spawn a tree too. Trees are spawning on water tiles as well though, so clearly I’ve misunderstood the code.

HexGrid2DArray.SetNumZeroed(GridWidth);
	HexTreeArray.SetNumZeroed(GridWidth); //My code
	for (int32 i = 0; i < HexGrid2DArray.Num(); i++) {
		HexGrid2DArray[i].SetNumZeroed(GridHeight);
	}

	for (int32 y = 0; y < GridHeight; y++) {
		for (int32 x = 0; x < GridWidth; x++) {
			const bool oddRow = y % 2 == 1;
			const float xPos = oddRow ? (x * TileHorizontalOffset) + OddRowHorizontalOffset : x * TileHorizontalOffset;
			const float yPos = y * TileVerticalOffset;

			TSubclassOf<AHexTile> TileToSpawn = GrassTile;
			if (FMath::RandRange(0.f, 1.f) <= ChanceOfWater) {
				TileToSpawn = WaterTile;
			}

			AHexTile* NewTile = GetWorld()->SpawnActor<AHexTile>(TileToSpawn, FVector(FIntPoint(xPos, yPos)), FRotator::ZeroRotator);
			NewTile->TileIndex = FIntPoint(x, y);
			NewTile->SetActorLabel(FString::Printf(TEXT("%d, %d"), x, y));
			HexGrid2DArray[x][y] = NewTile;

//All my code below
			if (TileToSpawn = GrassTile) {
				if (FMath::RandRange(0.f, 1.f) <= ChanceOfTree) {
					TSubclassOf<AHexTree> TreeToSpawn = PineTree;
					const float zPos = 35.0;
					AHexTree* NewTree = GetWorld()->SpawnActor<AHexTree>(TreeToSpawn, FVector(xPos, yPos, zPos), FRotator::ZeroRotator);
					NewTree->TreeIndex = FIntPoint(x, y);
					HexTreeArray.Add(NewTree);
				}
			}
		}
	}

This is basically all the code in the file at the moment. Everything works perfectly, except trees are spawning on water tiles, and I’m not sure what I’ve misunderstood.

1 Like

Rubber Duck Debugging.

If writing this post made you see the mistake, then the forum has done its job :smiley: