Hi there.
I am currently looking at a few videos on the last section on the UE4 C++ Couse (SimpleShooter) and I’m having trouble.
I have a MainCharacter.h/.cpp and a Handgun.h/.cpp file which I am trying to setup to create a gun. At this stage, I’m trying to attach the handgun to a socket. However, when coding this in I am getting the following error and I have no idea on how to solve it.
**GetMesh() displays "argument of type "USkeletalMeshComponent " is incompatible with parameter of type "USceneComponent "
// Fill out your copyright notice in the Description page of Project Settings.
#include "MainCharacter.h"
#include "Engine/Engine.h"
#include "GameFramework/Actor.h"
#include "Handgun.h"
#include "GameFramework/Character.h"
// Sets default values
AMainCharacter::AMainCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//Create a first person camera component
MainCharacterCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
check(MainCharacterCameraComponent != nullptr);
//Attach the camera component to our capsule component
MainCharacterCameraComponent->SetupAttachment(CastChecked<USceneComponent, UCapsuleComponent>(GetCapsuleComponent()));
//Position the camera slightly above the eyes
MainCharacterCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
//Enable the pawn to control camera rotation
MainCharacterCameraComponent->bUsePawnControlRotation = true;
}
// Called when the game starts or when spawned
void AMainCharacter::BeginPlay()
{
Super::BeginPlay();
//check(GEngine != nullptr);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("[DEBUG] - MainCharacter in use."));
Handgun = GetWorld()->SpawnActor<AHandgun>(HandgunClass);
Handgun->AttachToComponent(GetMesh(), FAttachedActorInfo::KeepRelativeTransform, TEXT("WeaponSocket"));
Handgun->SetOwner(this);
}
// Called every frame
void AMainCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
//Set up movement bindings.
PlayerInputComponent->BindAxis("MoveForward", this, &AMainCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMainCharacter::MoveRight);
//Set up Look Bindings
PlayerInputComponent->BindAxis("Turn", this, &AMainCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AMainCharacter::AddControllerPitchInput);
}
void AMainCharacter::MoveForward(float value)
{
//Find out which way is "forward" and reocrd that the player wants to move that way
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, value);
}
void AMainCharacter::MoveRight(float value)
{
//Find out which way is "right" and record that the player wants to move that way
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
AddMovementInput(Direction, value);
}
I have tried to just use GetMesh() on its own (on another line) and get the following error
pointer to incomplete class type is not allowed
I even downloaded the SimpleShooter project from GitHub and these errors are occurring on there too.
I have no clue to why these errors are appearing. I have been trying for hours to try and fix them, but no luck whatsoever.
MainCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/PlayerController.h"
#include "MainCharacter.generated.h"
class AHandgun;
UCLASS()
class MERCENARIES_API AMainCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMainCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(EditDefaultsOnly)
TSubclassOf<AHandgun> HandgunClass;
UPROPERTY()
AHandgun* Handgun;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//Handles Input for moving FORWARD and BACK
UFUNCTION()
void MoveForward(float value);
//Handles input for moving RIGHT and LEFT
UFUNCTION()
void MoveRight(float value);
//FPS camera
UPROPERTY(VisibleAnywhere)
UCameraComponent* MainCharacterCameraComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Gameplay)
FVector MuzzleOffset;
};
Handgun.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Handgun.h"
#include "Components/SkeletalMeshComponent.h"
#include "DrawDebugHelpers.h"
// Sets default values
AHandgun::AHandgun()
{
// 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;
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Mesh->SetupAttachment(Root);
}
// Called when the game starts or when spawned
void AHandgun::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AHandgun::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
//void AHandgun::Shoot()
//{
// FVector getPlayerViewLocation;
// FRotator getPlayerViewRotation;
// APawn* OwnerPawn = Cast<APawn>(GetOwner());
// if (OwnerPawn == nullptr) return;
// AController* OwnerController = OwnerPawn->GetInstigatorController();
// if (OwnerController == nullptr) return;
//
//
// GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(getPlayerViewLocation, getPlayerViewRotation);
//
// //Draw a line from the player
// FVector LineTraceEnd = getPlayerViewLocation + getPlayerViewRotation.Vector() * range;
//
// //Reach out to a certain distance
// FHitResult hit;
// FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());
//
// //Store the hit in a Variable
// bool bIsHit = GetWorld()->LineTraceSingleByChannel(hit, getPlayerViewLocation, LineTraceEnd, ECC_GameTraceChannel2, TraceParams);
//
// //Store the hit result
// AActor* HitActor = hit.GetActor();
// if (bIsHit)
// {
// DrawDebugLine(GetWorld(), getPlayerViewLocation, hit.Location, FColor(255, 0, 0), false, 3.0f, 0.0f, 5.0f);
// DrawDebugPoint(GetWorld(), hit.Location, 20.0f, FColor::Green, false, 3.0f, 0.0f);
// if (HitActor != nullptr)
// {
// FPointDamageEvent DamageEvent(power, hit, LineTraceEnd, nullptr);
// HitActor->TakeDamage(power, DamageEvent, OwnerController, this);
// }
// UE_LOG(LogTemp, Warning, TEXT("Actor hit is: %s"), *(HitActor->GetName()));
// }
//
// GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, TEXT("[WEAPON] - Headgun Fired."));
//
// capacity--;
// GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Magenta, FString::Printf(TEXT("Current Ammo: %i"), capacity));
//
// if (capacity <= 0)
// {
// GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Red, TEXT("Out of Ammo!"));
// }
//}
Handgun.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/ActorComponent.h"
#include "GameFramework/PlayerController.h"
#include "DrawDebugHelpers.h"
#include "Engine/Engine.h"
#include "Engine/EngineTypes.h"
#include "Handgun.generated.h"
UCLASS()
class MERCENARIES_API AHandgun : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AHandgun();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere)
USceneComponent* Root;
UPROPERTY(VisibleAnywhere)
USkeletalMeshComponent* Mesh;
//void Shoot();
UPROPERTY(EditAnywhere)
float power = 10.0f;
UPROPERTY(EditAnywhere)
int capacity = 10;
UPROPERTY(EditAnywhere)
float range = 10000.0f;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
If you’re wondering why there is lots of different includes, I’m patching things together to try and make it work. I understand most of them might not be necessary.
Any help on this issue would gratefully be appreciated!