Skip to content

Commit e16341a

Browse files
committed
Added spawning pickups from the 'SpawnVolume'
1 parent f62e457 commit e16341a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Source/BatteryCollector/SpawnVolume.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "BatteryCollector.h"
44
#include "SpawnVolume.h"
55
#include "Kismet/KismetMathLibrary.h"
6+
#include "Pickup.h"
67

78

89
// Sets default values
@@ -39,3 +40,33 @@ FVector ASpawnVolume::GetRandomPointInVolume()
3940

4041
}
4142

43+
void ASpawnVolume::SpawnPickup()
44+
{
45+
// If we have set something to spawn:
46+
if (WhatToSpawn != NULL)
47+
{
48+
// Check for a valid World:
49+
UWorld* const World = GetWorld();
50+
if (World)
51+
{
52+
// Set the spawn parameters
53+
FActorSpawnParameters SpawnParams;
54+
SpawnParams.Owner = this;
55+
SpawnParams.Instigator = Instigator;
56+
57+
// Get a random location to spawn at
58+
FVector SpawnLocation = GetRandomPointInVolume();
59+
60+
// Get a random rotation for the spawned item
61+
FRotator SpawnRotation;
62+
SpawnRotation.Yaw = FMath::FRand() * 360.0f;
63+
SpawnRotation.Pitch = FMath::FRand() * 360.0f;
64+
SpawnRotation.Roll = FMath::FRand() * 360.0f;
65+
66+
// spawn the pickup
67+
APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
68+
}
69+
}
70+
71+
}
72+

Source/BatteryCollector/SpawnVolume.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
2727
UFUNCTION(BlueprintPure, Category = "Spawning")
2828
FVector GetRandomPointInVolume();
2929

30+
protected:
31+
/** The pickup to spawn*/
32+
UPROPERTY(EditAnywhere, Category = "Spawning")
33+
TSubclassOf<class APickup> WhatToSpawn;
34+
3035
private:
3136
/** Box Component to specify where pickups should be spawned */
3237
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
3338
class UBoxComponent* WhereToSpawn;
39+
40+
/** Handle spawning a new pickup */
41+
void SpawnPickup();
3442

3543
};

0 commit comments

Comments
 (0)