[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English original Address

Following the section, in this tutorial we will add a Billboard component to our Actor object. It’s also easy to add components in the UE4 editor, but let’s do it programmatically this time.

First, we’ll create a new Actor subclass called AddBillboardComp. Remember, if you use a different name, make sure you change the name where you use the header file and the CPP file.

In the header file, we will create a variable that inherits from the UBillboardComponent class. This will allow us to add a billboard component and use its properties.

AddBillboardComp.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AddBillboardComp.generated.h"

UCLASS(a)class UNREALCPP_API AAddBillboardComp : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	AAddBillboardComp(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// declare point light comp
	UPROPERTY(VisibleAnywhere)
	class UBillboardComponent* MyBillboardComp;
	
};
Copy the code

In the CPP file, we will add the billboard component to this Actor class. The process for adding any component to an Actor class is very similar

If you want to use any component classes in actor, you must include the component header file in the CPP file. So, let’s add the bulletin board component file to the code.

#include "Components/BillboardComponent.h"
Copy the code

In this tutorial, we’ll add components to the constructor of an Actor subclass. This ensures that components are added to the actor when they are added to the scene.

Creates the default child object of the bulletin board component

MyBillboardComp = CreateDefaultSubobject<UBillboardComponent>(TEXT("Root Billboard Comp"));
Copy the code

And make it visible

MyBillboardComp->SetHiddenInGame(false.true);
Copy the code

Make the bulletin board component the root component

RootComponent = MyBillboardComp;
Copy the code

The final complete CPP code is shown below

#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(a)
{
	Super::BeginPlay(a); }// Called every frame
void AAddBillboardComp::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
Copy the code

The end result looks like this