I have a blueprint named “RifleProjectile_BP1” in my Content folder, with a parent class of “Actor”. I want to spawn an instance of this blueprint in the game world (at a specific location) when the player presses the left mouse button. It’s essentially a bullet that I want to spawn when the player fires their weapon.
How can I get C++ to recognize my blueprint class. I think I’m messed up a little bit on the syntax.
Here is my script .h file:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "RifleComp1.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class THIRDPER2_API URifleComp1 : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
URifleComp1();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
AActor* MainCharacter;
APlayerController* MainPlayerController1;
UPROPERTY(EditAnywhere)
AActor* RifleMuzzleFlash1;
UPROPERTY(EditAnywhere)
AActor* RifleBulletProjectile1;
UParticleSystemComponent* MuzzleFlashParticleComp1;
};
And here is the .cpp file:
#include "RifleComp1.h"
#include "GameFramework/Actor.h"
#include "Runtime/Engine/Classes/Particles/ParticleSystemComponent.h"
#include "Components/InputComponent.h"
#include "Engine/Blueprint.h"
#include "Engine/World.h"
// Sets default values for this component's properties
URifleComp1::URifleComp1()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void URifleComp1::BeginPlay()
{
Super::BeginPlay();
MainCharacter = GetWorld()->GetFirstPlayerController()->GetPawn();
MainPlayerController1 = GetWorld()->GetFirstPlayerController();
MuzzleFlashParticleComp1 = RifleMuzzleFlash1->FindComponentByClass<UParticleSystemComponent>();
}
// Called every frame
void URifleComp1::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// If the player presses the "Shoot1" button.
//if (MainPlayerController1->IsInputKeyDown(EKeys::LeftMouseButton))
if (MainPlayerController1->WasInputKeyJustPressed(EKeys::LeftMouseButton))
{
UE_LOG(LogTemp, Warning, TEXT("LeftMouseWasClicked!"));
if (MuzzleFlashParticleComp1 != nullptr)
{
MuzzleFlashParticleComp1->ActivateSystem(true);
// Spawn the bullet at the particle system location. The bullet blueprint (i.e. rifle projectile blueprint)
// already has a default "ProjectileMovement" script/component on it, so that should make the bullet
// move automatically when spawned.
if (RifleBulletProjectile1 != nullptr)
{
FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor<AActor>(ARifleProjectile_BP1, MuzzleFlashParticleComp1->GetLocation(), MuzzleFlashParticleComp1->GetRotation(), SpawnInfo);
}
}
}
}
In my .cpp file it will not recognize “RifleProjectile_BP1” as a class. But I don’t know how to get it to recognize it as a class, since it’s a blueprint I’m assuming.
Thanks for any help.