Add Component
Github Link: https://github.com/Harrison1/unrealcpp/tree/master/AddBillboardComp
For this tutorial we are using the standard first person C++ template with starter content. If you don't know how to add a new actor class to your project, please visit the Add C++ Actor Class post.
In this tutorial we are going to add a Billboard Component
to our actor. Adding components can also be easily added in the UE4 editor, but let's go ahead and do it programmatically.
First we'll create a new actor named AddBillboardComp
. Remember, if you call your actor something different, be sure to change the name everywhere in the header and cpp file.
In the header file we will create one variable that inherits from the UBillboardComponent
class. This will allow us to add a billboard component and use it's attributes.
AddBillboardComp.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AddBillboardComp.generated.h"
UCLASS()
class UNREALCPP_API AAddBillboardComp : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAddBillboardComp();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// declare point light comp
UPROPERTY(VisibleAnywhere)
class UBillboardComponent* MyBillboardComp;
};
In the .cpp
file we are going to add the Billboard Component
to our actor. This process is very similar to adding any component to an actor.
If you want to use any component class with you actor, you have to to include the component header file in the .cpp
file. So, let's add the Billboard Component
file to our code. Add all necessary scripts under the named components header include call.
Add BillboardComponent.h
#include "Components/BillboardComponent.h"
For this tutorial we are going to add the component in the actor's init function. This will ensure the component is added to the actor whenever it is added to a scene.
Create a default sub object of the billboard component
MyBillboardComp = CreateDefaultSubobject<UBillboardComponent>(TEXT("Root Billboard Comp"));
For this tutorial let's make it so we can see the billboard sprite in game
MyBillboardComp->SetHiddenInGame(false, true);
Make the billboard component the root component
RootComponent = MyBillboardComp;
Below is the final .cpp
file.
AddBillboardComp.h
#include "AddBillboardComp.h"
// include billboard comp
#include "Components/BillboardComponent.h"
// Sets default values
AAddBillboardComp::AAddBillboardComp()
{
// 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;
MyBillboardComp = CreateDefaultSubobject<UBillboardComponent>(TEXT("Root Billboard Comp"));
MyBillboardComp->SetHiddenInGame(false, true);
RootComponent = MyBillboardComp;
}
// Called when the game starts or when spawned
void AAddBillboardComp::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAddBillboardComp::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}