# UE4__C++基础

UE4__C++基础

初识C++类

创建类的标注目录:

D:/Documents/Unreal Projects/CodeTest/Source/CodeTest/Public/

头文件含义(组件用户在头文件注释中会有体现):

C++
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "ManinCharacter.generated.h"

UCLASS()
class CODETEST_API AManinCharacter : public ACharacter
    // Character前的A代表继承自Actor
{
	GENERATED_BODY()

public:
	// 构造函数
    // Sets default values for this character's properties
	AManinCharacter();

protected:
	// 开始加载时调用一次
    // Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
    // 每帧调用
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// 绑定键盘输入控制
    // Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};

反射与垃圾收集

Unreal Header Tool(UHT):建立反射的工具,他会扫描头文件,生成ManinCharacter.generated.h,用来帮助开放蓝图建立反射。

C++
	/** Networking - Server - TearOff this actor to stop replication to clients. Will set bTearOff to true. */
	UFUNCTION(BlueprintCallable, Category=Replication)
        // BlueprintCallable表示可以在蓝图中调用
	virtual void TearOff();

UFUNCTION:宏,在编译之后依然可以被调用。

创建一个UObject

头文件:

C++
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"

/**
 * 
 */
UCLASS(Blueprintable)
class CODETEST_API UMyObject : public UObject
{
	GENERATED_BODY()

public:
	
	UMyObject();
	
	UPROPERTY(BlueprintReadWrite, Category = "MyVariables")
        // 反射,使蓝图可以读取,并设置其目录,可以写成BlueprintReadOnly变成只读
	float MyFloat;
	
	UFUNCTION(BlueprintCallable, Category = "MyFunctions")
        // 反射,使蓝图可以调用
	void MyFunction();

};

cpp:

C++
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyObject.h"
#include "..\Public\MyObject.h"

UMyObject::UMyObject()
{
	MyFloat = 0;
}
void UMyObject::MyFunction()
{
    UE_LOG(LogTemp, Warning, TEXT("这是一个警告!"));
}

Actor

头文件

C++
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class CODETEST_API AFloater : public AActor
    // 继承了Actor中的可蓝图化
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFloater();
    //加入静态网格体组件,并设置分类,形式是指针
	UPROPERTY(VisibleAnywhere, Category = "ActorMeshComponents")
		UStaticMeshComponent* StaticMesh;
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

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

};

cpp:

C++
// Fill out your copyright notice in the Description page of Project Settings.


#include "Floater.h"

// Sets default values
AFloater::AFloater()
{
 	// 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;

	StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));
    //创建一个默认的子物体,类型为UStaticMeshComponent,名字为CustomStaticMesh。
}

// Called when the game starts or when spawned
void AFloater::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

FVector

C++
		// 场景中编辑(Actor蓝图细节里没有),蓝图可读写
	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = "Floater Variables")
		FVector InitialLocation;


		/// Indicates that this property can be edited by property windows in the editor
		EditAnywhere,

		/// Indicates that this property can be edited by property windows, but only on instances, not on archetypes
		EditInstanceOnly,

		/// Indicates that this property can be edited by property windows, but only on archetypes
		EditDefaultsOnly,

		/// Indicates that this property is visible in property windows, but cannot be edited at all
		VisibleAnywhere,
		
		/// Indicates that this property is only visible in property windows for instances, not for archetypes, and cannot be edited
		VisibleInstanceOnly,

		/// Indicates that this property is only visible in property windows for archetypes, and cannot be edited
		VisibleDefaultsOnly,

		/// This property can be read by blueprints, but not modified.
		BlueprintReadOnly,

		/// This property has an accessor to return the value. Implies BlueprintReadOnly if BlueprintSetter or BlueprintReadWrite is not specified. (usage: BlueprintGetter=FunctionName).
		BlueprintGetter,

		/// This property can be read or written from a blueprint.
		BlueprintReadWrite,

		/// This property has an accessor to set the value. Implies BlueprintReadWrite. (usage: BlueprintSetter=FunctionName).
		BlueprintSetter,

Vector.h中会注明构造Vector的方式:

C++
/**
	 * Constructor initializing all components to a single float value.
	 *
	 * @param InF Value to set all components to.
	 */
	explicit FORCEINLINE FVector(float InF);

	/**
	 * Constructor using initial values for each component.
	 *
	 * @param InX X Coordinate.
	 * @param InY Y Coordinate.
	 * @param InZ Z Coordinate.
	 */
	FORCEINLINE FVector(float InX, float InY, float InZ);

	/**
	 * Constructs a vector from an FVector2D and Z value.
	 * 
	 * @param V Vector to copy from.
	 * @param InZ Z Coordinate.
	 */
	explicit FORCEINLINE FVector(const FVector2D V, float InZ);

	/**
	 * Constructor using the XYZ components from a 4D vector.
	 *
	 * @param V 4D Vector to copy from.
	 */
	FORCEINLINE FVector(const FVector4& V);

	/**
	 * Constructs a vector from an FLinearColor.
	 *
	 * @param InColor Color to copy from.
	 */
	explicit FVector(const FLinearColor& InColor);

	/**
	 * Constructs a vector from an FIntVector.
	 *
	 * @param InVector FIntVector to copy from.
	 */
	explicit FVector(FIntVector InVector);

	/**
	 * Constructs a vector from an FIntPoint.
	 *
	 * @param A Int Point used to set X and Y coordinates, Z is set to zero.
	 */
	explicit FVector(FIntPoint A);

	/**
	 * Constructor which initializes all components to zero.
	 *
	 * @param EForceInit Force init enum
	 */
	explicit FORCEINLINE FVector(EForceInit);

然后一般在构造函数中设置其初始值。

Sweep

C++
直接打印为浮点数:
UE_LOG(LogTemp, Warning,
				TEXT("%%f Hit@: x=%f, y=%f, z=%f"),
				Hitresult.Location.X, Hitresult.Location.Y, Hitresult.Location.Z);
打印为F-String字符串:
UE_LOG(LogTemp, Warning,
	TEXT("%%s Hit@: x=%s, y=%s, z=%s"),
	*FSting::SanitizeFloat(Hitresult.Location.X), 
	*FSting::SanitizeFloat(Hitresult.Location.Y), 
 	*FSting::SanitizeFloat(Hitresult.Location.Z));

FHitResult接口:FHitResult Hitresult就可以直接把FHitResult的值赋给后面的变量。

函数AddActorLocalOffsetAddActorLocalOffset(FVector DeltaLocation, bool bSweep, FHitResult* OutSweepHitResult, ETeleportType Teleport),和蓝图里的AddActorLocalOffset一样。

力和扭矩

cpp文件中使用StaticMesh->AddForce(InitialForce)这种语句调用对象的方法时,需要加入这个对象的头文件。而具体哪个头文件则可以转到定义查看。(实际使用发现即使没有这个头文件也可以正常使用,检查父类确实也没有include过这个头文件,总之我先放一个问号在这里)

数学函数

FMath::就可以查找常用的数学函数。

删除C++类

1、到目录下删除头文件和cpp文件

2、删除项目目录下的Binaries文件夹(保存的一些已经编译好的文件)

3、重新生成vs文件,重新编译选是

最后编辑于
文章链接: http://pheustal.com/2021/01-01/Ue4_C%2B%2B
本作品采用CC-BY-SA许可。