“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

1. Header file analysis

  • CoreMinimal. H: Contains a set of common types (including FString, FName, TArray, etc.) files from the UE4 core programming environmentUE_4. 27 \ Engine \ Source \ Runtime \ Core \ PublicContains multiple engine header files. Include the minimum required header files together to form a header file
  • GameFramework: GameFramework file
  • Actor.h: actor. h contains the parent class AActor of actors
  • Myactor.generated. H: Must contain reflection mechanism for UE4 at the bottom of all header files
  • UCLASS() : Indicates that AMyActor class can be recognized by unreal engine iv, indicating that the class must be recognized by unreal Engine IV, and has the reflection mechanism of UE4 classes
  • Override: Indicates that the virtual function should be overridden
#pragma once

#include "CoreMinimal.h"//CoreMinimal.h: Contains a set of common types (including FString, FName, TArray, etc.) from the UE4 Core programming environment. The directory located in 'ue_4.27 \Engine\Source\Runtime\Core\Public' contains multiple Engine header files. Include the minimum required header files together to form a header file
#include "GameFramework/Actor.h"// Game frame, actor.h contains the parent class AActor of AMyActor
#include "MyActor.generated.h"

UCLASS(a)//UCLASS() : indicates that the AMyActor class is recognized by Unreal engine
class COMPANY_API AMyActor : public AActor//COMPANY_API is the project name
{
	GENERATED_BODY(a)//UE4 generates things
	
public:	
	// Sets default values for this actor's properties
	AMyActor(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;//override indicates that the virtual function is overridden

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

};
Copy the code
  • PrimaryActorTick. BCanEverTick: frame by frame calls Tick ()
  • Super: equivalent to the parent domain name super: :BeginPlay(); Equivalent to AActor: : BeginPlay ();
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"

// Sets default values
AMyActor::AMyActor()
{
 	// 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;// Call Tick() frame by frame

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay(a)
{	//super is equivalent to the parent domain name
	Super::BeginPlay(a);/ / equivalent to the
    //AActor::BeginPlay();
	
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
Copy the code

2, source code analysis of the introductory tutorial

VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));// Create a static mesh body called mesh
Copy the code

Source code as follows: a generic template

template<class TReturnType>
TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
{
    UClass* ReturnType = TReturnType::StaticClass(a);//CreateDefaultSubobject requires a pointer of type UClass
    return static_cast<TReturnType*>(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, bTransient));// The type is strongly converted to a template type
}
Copy the code

Declare a static object of type FObjectFinder in ConstructorHelpers template class UStaticMesh, Object reference called CubeVisualAsset parameters for shape file ‘/ Game/StarterContent/Shapes/Shape_Sphere Shape_Sphere’ similar to the directory

static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
Copy the code

Check whether the vm is created successfully

if (CubeVisualAsset.Succeeded())// Check whether the vm is successfully created
	{
		VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
		VisualMesh->SetRelativeLocation(FVector(0.0 f.0.0 f.0.0 f));// Set the relative position
	}
Copy the code

Gets the location of the current actor. I think it’s clearer to add this

FVector NewLocation = this->GetActorLocation(a);Copy the code

Get the Angle of the current actor. I think it’s clearer to add this

FRotator NewRotation = this->GetActorRotation(a);Copy the code

Get how long the game has been running since the object was created

float RunningTime = this->GetGameTimeSinceCreation(a);Copy the code

A since function is between -1 and 1

	float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
Copy the code

We’re moving in the z direction

	NewLocation.Z += DeltaHeight * 20.0 f;       //Scale our height by a factor of 20
Copy the code

20 degrees per second,pitch about y, yaw about z, roll about x axis,x: roll, roll Angle, with x as axis, yz plane rotation; Y: pitch, pitching Angle, y axis z: yaw, heading Angle, Z axis

float DeltaRotation = DeltaTime * 20.0 f;  
NewRotation.Yaw += DeltaRotation;/ / z weeks rotation
Copy the code

Set the location

this->SetActorLocationAndRotation(NewLocation, NewRotation);
Copy the code