This looks fine, for the projectile. But where is the code that calls SpawnActor to actually create it and put it in the scene?
Also, if you Create a new project, pick C++ project, and base it on the First Person example, it will generate all the C++ code needed to do what you’re asking. I’m fairly certain the tutorial you’re following was incorporated into that template. And even if you want to continue learning with your manually built project, it would still serve as a good reference project with a working example.
I’ve been following the UE4 First Person C++ Tutorial, and so far, I have been able to figure out the things that the tutorial left out (since it was made in 4.15). But I have been trying this one for awhile, and I cant seem to figure out the problem. I know that it has to do with the setup of my projectile’s movement parameters, but nothing I seem to do works. The project name is LearningCurve. Is there something that I’m missing?
LearningCurveProjectile.cpp
#include "LearningCurveProjectile.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
// Sets default values
ALearningCurveProjectile::ALearningCurveProjectile()
{
// 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;
// Use a sphere as simple collision representation
CollisionComponent = CreateDefaultSubobject<USphereComponent>(TEXT("SphereComponent"));
// Set the collision radius
CollisionComponent->InitSphereRadius(15.0f);
// Set root component to collision component
RootComponent = CollisionComponent;
// Use this component to drive this projectile's movement.
ProjectileMovementComponent = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComponent"));
ProjectileMovementComponent->SetUpdatedComponent(CollisionComponent);
ProjectileMovementComponent->InitialSpeed = 3000.0f;
ProjectileMovementComponent->MaxSpeed = 3000.0f;
ProjectileMovementComponent->bRotationFollowsVelocity = true;
ProjectileMovementComponent->bShouldBounce = true;
ProjectileMovementComponent->Bounciness = 0.3f;
}
// Called when the game starts or when spawned
void ALearningCurveProjectile::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ALearningCurveProjectile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Function that initializes the projectile's velocity in the shoot direction.
void ALearningCurveProjectile::FireInDirection(const FVector& ShootDirection)
{
ProjectileMovementComponent->Velocity = ShootDirection * ProjectileMovementComponent->InitialSpeed;
}
LearningCurveProjectile.h
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LearningCurveProjectile.generated.h"
UCLASS()
class LERANINGCURVE_API ALearningCurveProjectile : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALearningCurveProjectile();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Sphere collision component.
UPROPERTY(VisibleDefaultsOnly, Category = Projectile)
USphereComponent* CollisionComponent;
// Projectile movement component.
UPROPERTY(VisibleAnywhere, Category = Movement)
UProjectileMovementComponent* ProjectileMovementComponent;
// Function that initializes the projectile's velocity in the shoot direction.
void FireInDirection(const FVector& ShootDirection);
};
It’s not exactly a problem with spawning the projectile, but more just setting up the projectile (the error was occurring before I put in the firing mechanic).
Ah, I see. So you’ve correctly included the headers for the UProjectileMovementComponent and USphereComponent in your LearningCurveProjectile.cpp, but you haven’t forward declared those classes in the header that specifies them. Add these lines to LearningCurveProjectile.h after your included headers:
class USphereComponent;
class UProjectileMovementComponent;
When you get an error like “missing type specifier”, try to look at the line it refers to (28) and figure out why the code doesn’t know what that type is. Generally you either need to have an include file pointing to the type’s definition, or forward declare the type (if it’s only being used as a pointer and you’re planning to include the header in a different source file)