Ch4 Actors and Components¶
-
AActor() base¶
A default Actor Class look like this
in header file MyFirstActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyFirstActor.generated.h"
UCLASS()
class CHAPTER_04_API AMyFirstActor : public AActor
{
GENERATED_BODY()
public:
//Set default values for this actor's properities
AMyFirstActor();
protected:
//Called when the game started or when spawned
virtual void BeginPlay() override;
public:
//Called every frame
virtual void tick(float DeltaTime) override;
}
MyFirstActor.cpp
#include "AMyFirstActor.h"
AMyFirstActor::AMyfirstActor()
{
// set this actor to call Tick() every frame, can be turned off
PrimaryActorTick.bCanEverTick = true;
}
//Called when game start or spawned
void AMyFirstActor::BeginPlay()
{
Super::BeginPlay();
}
void AMyFirstActor::Tick(float DeltaTime)
{
super::Tick(DeltaTime);
}
-
Destroy AActor¶
Destroy by time
setting timer at BeginPlay() function
FTimerHandle Timer;
GetWorldTimeManager.SetTimer(Timer, this,
&ACustomGameModeBase::DestroyActorFunction, 10)
void ACustomGameModeBase::DestroyActorFunction()
{
if(SpawnedActor != nullptr)
{
//Display message on screen
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("Actor Destroyed"));
SpawnedActor->Destroy();
}
}
-
Actor components¶
Mesh of Actor¶
//in header file
UPROPERTY()
UStaticMeshComponent *Mesh;
//in cpp file
AMyFirstActor::AMyFirstActor()
{
//add this line
Mesh = CreateDefaultSubobject<UStaticMeshComponent>("BaseMeshComponent");
}
Assets loading¶
after mesh is created, we can load assets from content directory
auto MeshAsset = ConstructoHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.Cube'"));
// Check if the MeshAsset is valid before setting it
if(MeshAsset.Object != nullptr)
{
Mesh->SetStaticMesh(MeshAsset.Object);
}
Components attaching¶
let’s say that we have multiple mesh and assets need to bind together
in header file HierachyActorh.
UPROPERTY(VisibleAnywhere)
USceneComponent *Root;
UPROPERTY(VisibleAnyWhere)
USceneComponent *ChildSceneComponent;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent *BoxOne;
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent *BoxTwo;
in cpp file
// create subobject
Root = CreateDefaultSubobject<USceneComponent>("Root");
ChildSceneComponent = CreateDefaultSubobject<USceneComponent>("ChildSceneComponent");
BoxOne = CreateDefaultSubobject<UStaticMeshComponent>("BoxOne");
BoxTwo = CreateDefaultSubobject<UStaticMeshComponent>("BoxTwo");
// get a reference to the cube mesh
auto MeshAsset = ConstructHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cube.cube'"));
//Give both boxes a mesh
if(MeshAsset.Object != nullptr)
{
BoxOne->SetStaticMesh(MeshAsset.Object);
BoxTwo->SetStaticMesh(MeshAsset.Object);
}
RootComponent = Root;
//set up the object's hierarchy
BoxOne->AttachTo(Root);
BoxTwo->AttachTo(ChildSceneComponent);
ChildSceneComponent->AttachTo(Root);
// offset and scale the child from the root
ChildSceneComponent->SetRelativeTransform(
FTransform(FRotator(0,0,0),
FVector(250,0,0),
FVector(0.1f))
);