Hello There!!
In this lecture, we create some components in the header file and set up their hierarchy in the code file.
Header file-
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PawnBase.generated.h"
class UCapsuleComponent;
UCLASS()
class TOONTANKS_API APawnBase : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APawnBase();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
private:
UPROPERTY()
UCapsuleComponent* CapsuleComp;
UPROPERTY()
UStaticMeshComponent* BaseMesh;
UPROPERTY()
UStaticMeshComponent* TurretMesh;
UPROPERTY()
USceneComponent* ProjectileSpawnPoint;
};
Let’s discuss the classes in the private section, correct me anywhere if I’m wrong-
UCapsuleComponent*-
It’s just a simple collision for the base mesh.
Documentation - UCapsuleComponent | Unreal Engine Documentation
UStaticMeshComponent*-
This is a reference to UStaticMesh. Static Mesh is for a mesh that’s not going to move. This is for our base mesh and the turret above.
Documentation-
UStaticMeshComponent*-
UStaticMesh(UStaticMeshComponent references to this)-
USceneComponent*-
This has transform and it supports attachment but has no rendering or collision capabilities.
Documentation-
C++ file-
// Fill out your copyright notice in the Description page of Project Settings.
#include "PawnBase.h"
#include "Components/Capsule.h"
// Sets default values
APawnBase::APawnBase()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CapsuleComp = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule Collider"));
RootComponent = CapsuleComp;
BaseMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base Mesh"));
BaseMesh->SetupAttachment(RootComponent);
TurretMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Base TurretMesh"));
TurretMesh->SetupAttachment(BaseMesh);
ProjectileSpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("Projectile Spawn Point"));
ProjectileSpawnPoint->SetupAttachment(TurretMesh);
}
// Called when the game starts or when spawned
void APawnBase::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APawnBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APawnBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
Now, in my constructor, CupsuleComp is the top class(every child class inherits from it).
A new function here is CreateDefaultSubobject, this will create a component and allow you to create child classes and return the parent class.
Documentation - UObject::CreateDefaultSubobject | Unreal Engine Documentation
Another new function is SetupAttachment. This function will attach a parent class to the component.
Documentation - USceneComponent::SetupAttachment | Unreal Engine Documentation
I’ve discussed a lot over here. Correct if I’m wrong anywhere.
Previous Post-Blueprint project to C++ project (Toon-Tanks)
Thanks for reading,
Good Bye!!