Hello guys,
So I’ve been working on a project for a bit now, and I’ve hit somewhat of a painful snag. I’m making an FPS and am in the process of creating a weapon spawner. I have the core spawner class created in C++, but I would like to make different types of spawners (ie. Weapon, Pickup, and Objective) via blueprints.
Currently I have the following code for my game’s spawner class:
MNSpawner.h
#pragma once
#include "GameFramework/Actor.h"
#include "MNPickup.h"
#include "MNSpawner.generated.h"
/**
*
*/
UCLASS()
//Abstract, BlueprintType, Blueprintable, Config = Engine
class MONKEYNUTS_API AMNSpawner : public AActor
{
GENERATED_UCLASS_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Spawning)
TSubobjectPtr<UBillboardComponent> EditorMarker;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Spawning)
TSubobjectPtr<USceneComponent> SceneComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
UClass* ObjectToSpawn;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
TSubclassOf<AMNPickup> ClassToSpawn;
//Float variable containing the amount of time it takes for an item to respawn
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
float fSpawnTime;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
FVector SpawnerLocation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
FRotator SpawnerRotation;
//Float containing the amount of time (in seconds) until the object spawns
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Spawning)
float fTimeTilSpawn;
UFUNCTION(BlueprintCallable, Category = Spawning)
void SpawnObject(UClass* ThingToSpawn, FVector SpawnerPos, FRotator SpawnerRot);
UFUNCTION()
virtual void Tick(float DeltaSeconds) override;
};
MNSpawner.cpp
#include "MonkeyNuts.h"
#include "MNSpawner.h"
AMNSpawner::AMNSpawner(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
SceneComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("SceneComponent"));
EditorMarker = PCIP.CreateDefaultSubobject<UBillboardComponent>(this, TEXT("EditorMarker"));
RootComponent = SceneComponent;
EditorMarker->AttachTo(RootComponent);
FVector SpawnerLocation = AActor::GetActorLocation();
FRotator SpawnerRotation = AActor::GetActorRotation();
}
void AMNSpawner::SpawnObject(UClass* ThingToSpawn, FVector SpawnerPos, FRotator SpawnerRot){
UWorld* CurrentWorld = GEngine->GetWorld();
if (CurrentWorld != NULL){
if (ThingToSpawn != NULL){
FActorSpawnParameters SpawnParameters;
CurrentWorld->SpawnActor<AMNPickup>(ThingToSpawn, SpawnerPos, SpawnerRot, SpawnParameters);
}
}
}
void AMNSpawner::Tick(float DeltaSeconds){
if (fTimeTilSpawn <= 0){
SpawnObject(ObjectToSpawn, SpawnerLocation, SpawnerRotation);
}
}
I have additionally attached my Weapon Spawner blueprint (BP_MNWeaponSpawner) . I really don’t understand what’s going on here. Can anyone explain why this crashing might be happening?