I am continuing the Battery Collector in C++ tutorial from Unreal Engine. This is the 6th video.
We will create a Spawning Volume with functionalities to setup:
- Where to Spawn
- What to Spawn
- When to Spawn
There are 3 videos for each functionality.
Creating a Spawning Volume
Before going on, I delete the pickups in the level. Then I create a new C++ class based on Actor and named SpawnVolume:
In the SpawnVolume.h file, I add a UBoxComponent pointer named WhereToSpawn:
private: /** Box Component to specify where pickups should be spawned */ UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true")) class UBoxComponent* WhereToSpawn;
In the public section, I add an accessor function:
/** Returns the WhereToSpawn subobject */ FORCEINLINE class UBoxComponent* GetWhereToSpawn() const { return WhereToSpawn; }
Note: in the Pickup.h file, we didn’t use a FORCEINLINE macro for IsActive() because BlueprintPure functions are not compatible with inline macros.
In the constructor of SpawnVolume.cpp, I create the box and set it as the RootComponent:
// Create the Box Component to represent the spawn volume WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn")); RootComponent = WhereToSpawn;
In that constructor, I also disable the call to Tick() every frame:
PrimaryActorTick.bCanEverTick = false;
Back to the header file, I declare a GetRandomPointInVolume public function:
/** Find a random point within the BoxComponent */ UFUNCTION(BlueprintPure, Category = "Spawning") FVector GetRandomPointInVolume();
In the source file, I include the KismetMathLibrary to get access to random math functions:
#include "Kismet/KismetMathLibrary.h"
Then I create the GetRandomPointInVolume definition:
FVector ASpawnVolume::GetRandomPointInVolume() { FVector SpawnOrigin = WhereToSpawn->Bounds.Origin; FVector SpawnExtent = WhereToSpawn->Bounds.BoxExtent; return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent); }
Instead of compiling in Unreal Engine editor, I can build in Visual Studio:
Next Step
In the next step, we will define what to spawn.
Leave a Reply