Can somebody help me? I’m making a FPS, and I’m trying to create a weapon spawner, but for some reason this code makes my Unreal Engine crash every time I try to open my project.
WeaponSpawner.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
#include "Components/PointLightComponent.h"
#include "Components/BoxComponent.h"
#include "WeaponSpawner.generated.h"
UCLASS()
class COOPFPS_API AWeaponSpawner : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AWeaponSpawner();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Components
UPROPERTY(EditAnywhere)
UStaticMeshComponent* SpawnerMesh;
UPROPERTY(EditAnywhere)
UPointLightComponent* SpawnerLight;
UPROPERTY(EditAnywhere)
UBoxComponent* SpawnerCollision;
// Variables
int WeaponToSpawn;
float CooldownCounter = 0.0f;
bool ShouldSpawn = true;
// Functions
UFUNCTION()
void SpawnWeapon();
UFUNCTION()
void Collide(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};
WeaponSpawner.cpp:
.
#include "WeaponSpawner.h"
#include "PickablePistol.h"
#include "PickablePlasmaPistol.h"
#include "PickableWeapon.h"
// Sets default values
AWeaponSpawner::AWeaponSpawner()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh> _SpawnerMesh(TEXT("StaticMesh'/Game/Meshes/Weapons/WeaponSpawner/WeaponSpawner.WeaponSpawner'"));
SpawnerCollision->CreateDefaultSubobject<UBoxComponent>(TEXT("Weapon Spawner's Collision"));
RootComponent = SpawnerCollision;
SpawnerCollision->SetBoxExtent(FVector(1000.0f, 1000.0f, 2000.0f));
SpawnerCollision->OnComponentBeginOverlap.AddDynamic(this, &AWeaponSpawner::Collide);
SpawnerMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Weapon Spawner's Mesh"));
SpawnerLight->SetupAttachment(RootComponent);
SpawnerMesh->SetStaticMesh(_SpawnerMesh.Object);
SpawnerMesh->SetWorldScale3D(FVector(0.1f, 0.1f, 0.1f));
SpawnerLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("Weapon Spawner's Light"));
SpawnerLight->SetupAttachment(RootComponent);
SpawnerLight->SetRelativeLocation(FVector(0.0f, 0.0f, 250.0f));
SpawnerLight->SetLightColor(FLinearColor::Red);
}
// Called when the game starts or when spawned
void AWeaponSpawner::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AWeaponSpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
CooldownCounter -= 1.0f;
if (CooldownCounter <= 0.0f)
{
SpawnWeapon();
//CooldownCounter = FMath::RandRange(1800, 7200);
CooldownCounter = 60.0f;
}
}
void AWeaponSpawner::SpawnWeapon()
{
if (ShouldSpawn)
{
WeaponToSpawn = FMath::RandRange(1, 2);
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
FVector SpawnLocation(GetActorLocation() + FVector(0.0f, 0.0f, 100.0f));
FRotator SpawnRotation(0.0f, 0.0f, 0.0f);
if (WeaponToSpawn == 1) // Pistol
{
TSubclassOf<class APickablePistol> PistolSpawn = APickablePistol::StaticClass();
APickablePistol* const SpawnedWeapon = GetWorld()->SpawnActor<APickablePistol>(PistolSpawn, SpawnLocation, SpawnRotation, SpawnParams);
}
else if (WeaponToSpawn == 2) // Plasma Pistol
{
TSubclassOf<class APickablePlasmaPistol> PlasmaPistolSpawn = APickablePlasmaPistol::StaticClass();
APickablePlasmaPistol* const SpawnedWeapon = GetWorld()->SpawnActor<APickablePlasmaPistol>(PlasmaPistolSpawn, SpawnLocation, SpawnRotation, SpawnParams);
}
}
}
void AWeaponSpawner::Collide(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor->GetClass()->IsChildOf(APickableWeapon::StaticClass())) ShouldSpawn = false;
else ShouldSpawn = true;
}