Download

Help with 'Introduction to UE4 Programming'

Hi,

I just finished watching all the videos in this series: https://www.youtube.com/watch?v=vtcWrcscXos&index=1&list=PLZlv_N0_O1gb5xvsc7VM7pfoRAKLuIcFi

My project seems to be functioning as intended except for one thing: the batteries only fall in region, even though the SpawnVolume encompasses the whole map.
I have been back through the videos to find where I messed up, but I just can’t find anything. Does anyone have any ideas?

This is what happens:

https://youtube.com/watch?v=U2f1geIu4_c

The contents of SpawnVolume cpp and header files:




#include "TutorialCode.h"
#include "SpawnVolume.h"
#include "Pickup.h"

ASpawnVolume::ASpawnVolume(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	/** Create the simple StaticMeshComponent to represent the pickup in the level */
	WhereToSpawn = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("WhereToSpawn"));

	// Set the StaticMeshComponent as the root component
	RootComponent = WhereToSpawn;

	// Set the spawn delay range and get the first SpawnDelay
	SpawnDelayRangeLow = 1.0f;
	SpawnDelayRangeHigh = 4.5f;
	SpawnDelay = GetRandomSpawnDelay();

	// Make the SpawnVolume tickable
	PrimaryActorTick.bCanEverTick = true;

}

void ASpawnVolume::SpawnPickup()
{
	// If we have set something to spawn:
	if (WhatToSpawn != NULL)
	{
		// Check for a valid World:
		UWorld* const World = GetWorld();
		if (World)
		{
			// Set the spawn parameters
			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = Instigator;

			// Get a random location to spawn at
			FVector SpawnLocation = GetRandomPointInVolume();

			// Get a random rotation for the spawned item
			FRotator SpawnRotation;
			SpawnRotation.Yaw = FMath::FRand() * 360.f;
			SpawnRotation.Pitch = FMath::FRand() * 360.f;
			SpawnRotation.Roll = FMath::FRand() * 360.f;

			// Spawn the pickup
			APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);

		}
	}
}

float ASpawnVolume::GetRandomSpawnDelay()
{
	// Get a random float that falls within the spawn delay range
	return FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);

}

FVector ASpawnVolume::GetRandomPointInVolume()
{
	FVector RandomLocation;
	float MinX, MinY, MinZ;
	float MaxX, MaxY, MaxZ;

	FVector Origin;
	FVector BoxExtent;

	// Get the SpawnVolume's origin and box extent
	Origin = WhereToSpawn->Bounds.Origin;
	BoxExtent = WhereToSpawn->Bounds.BoxExtent;

	// Calculate the minimum X, Y, and Z
	MinX = Origin.X - BoxExtent.X / 2.f;
	MinY = Origin.Y - BoxExtent.Y / 2.f;
	MinZ = Origin.Z - BoxExtent.Z / 2.f;

	// Calculate the maximum X, Y, and Z
	MaxX = Origin.X - BoxExtent.X / 2.f;
	MaxY = Origin.Y - BoxExtent.Y / 2.f;
	MaxZ = Origin.Z - BoxExtent.Z / 2.f;

	// The random spawn location will fall between the min and max X, Y, and Z
	RandomLocation.X = FMath::FRandRange(MinX, MaxX);
	RandomLocation.Y = FMath::FRandRange(MinY, MaxY);
	RandomLocation.Z = FMath::FRandRange(MinZ, MaxZ);

	// Return the random spawn location
	return RandomLocation;

}

void ASpawnVolume::Tick(float DeltaSeconds)
{
	// If spawning is not enabled, do nothing
	if (!bSpawningEnabled)
		return;

	// Always add delta time to our Spawn Time
	SpawnTime += DeltaSeconds;

	bool bShouldSpawn = (SpawnTime > SpawnDelay);

	if (bShouldSpawn)
	{
		SpawnPickup();

		// Deduct spawn delay from accumulated time value
		SpawnTime -= SpawnDelay;
		SpawnDelay = GetRandomSpawnDelay();
	}
}

void ASpawnVolume::EnableSpawning()
{
	bSpawningEnabled = true;
}

void ASpawnVolume::DisableSpawning()
{
	bSpawningEnabled = false;
}




#pragma once

#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"

/**
 * 
 */
UCLASS()
class ASpawnVolume : public AActor
{
	GENERATED_UCLASS_BODY()

	/** BoxComponent to specify the spawning area within the level */
	UPROPERTY(VisibleInstanceOnly, Category = Spawning)
	TSubobjectPtr<UBoxComponent> WhereToSpawn;

	/** The pickup to spawn */
	UPROPERTY(EditAnywhere, Category = Spawning)
	TSubclassOf<class APickup> WhatToSpawn;

	/** Minimum spawn delay */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
	float SpawnDelayRangeLow;

	/** Maximum spawn delay */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
	float SpawnDelayRangeHigh;

	/**  Finds a random point within the BoxComponent*/
	UFUNCTION(BlueprintPure, Category = Spawning)
	FVector GetRandomPointInVolume();

	virtual void Tick(float DeltaSeconds) OVERRIDE;

	void EnableSpawning();

	void DisableSpawning();

private:

	// Whether spawning is enabled
	bool bSpawningEnabled;

	/** Calculates a random spawn delay */
	float GetRandomSpawnDelay();

	/** The current spawn delay */
	float SpawnDelay;

	/** Handles the s pawning of a new pickup */
	void SpawnPickup();

	/** The timer for when to spawn the pickup */
	float SpawnTime;

};


Please say if you want to see the code for some other files.

Good job with the tutorial by the way. I look forward to new ones. :slight_smile:

It appears you’re setting the maximum spawn coordinates to the same values as the minimum.
This:




	// Calculate the maximum X, Y, and Z
	MaxX = Origin.X - BoxExtent.X / 2.f;
	MaxY = Origin.Y - BoxExtent.Y / 2.f;
	MaxZ = Origin.Z - BoxExtent.Z / 2.f;

should be:



	// Calculate the maximum X, Y, and Z
	MaxX = Origin.X + BoxExtent.X / 2.f;
	MaxY = Origin.Y + BoxExtent.Y / 2.f;
	MaxZ = Origin.Z + BoxExtent.Z / 2.f;

I think this will solve your problem =).

Yep, that was it. Thanks!
I thought it would be something really obvious like that :rolleyes: