此为udemy课程unreal-engine-5-the-ultimate-game-developer-course第6节Moving Object With Code的学习笔记
52、setactorlocation
void AItem::BeginPlay()
{Super::BeginPlay();UWorld* World = GetWorld();SetActorLocation(FVector(0.f, 0.f, 50.f));FVector Location = GetActorLocation();FVector Forward = GetActorForwardVector();DRAW_SPHERE(Location);DRAW_VECTOR(Location, Location + Forward * 100.f);}
53、set actor rotation
void AItem::BeginPlay()
{Super::BeginPlay();UWorld* World = GetWorld();SetActorLocation(FVector(0.f, 0.f, 50.f));SetActorRotation(FRotator(0.f, 45.f, 0.f));FVector Location = GetActorLocation();FVector Forward = GetActorForwardVector();DRAW_SPHERE(Location);DRAW_VECTOR(Location, Location + Forward * 100.f);}
54、actor world offset
--------------DebugMacros.h-------------#pragma once
#include "DrawDebugHelpers.h"
#define DRAW_SPHERE(Location) if (GetWorld()) DrawDebugSphere(GetWorld(), Location, 25.f, 24, FColor::Red, false, 30);
#define DRAW_SPHERE_SingleFrame(Location) if (GetWorld()) DrawDebugSphere(GetWorld(), Location, 25.f, 24, FColor::Red, false, -1.f);#define DRAW_LINE(StartLocation, EndLocation) if (GetWorld()) DrawDebugLine(World, StartLocation,EndLocation, FColor::Red, true, -1.f, 0, 2.f);
#define DRAW_LINE_SingleFrame(StartLocation, EndLocation) if (GetWorld()) DrawDebugLine(World, StartLocation,EndLocation, FColor::Red, false, -1.f, 0, 2.f);#define DRAW_POINT(Location) if (GetWorld()) DrawDebugPoint(GetWorld(), Location, 15.f, FColor::Red, true);
#define DRAW_POINT_SingleFrame(Location) if (GetWorld()) DrawDebugPoint(GetWorld(), Location, 15.f, FColor::Red, false, -1.f);#define DRAW_VECTOR(StartLocation, EndLocation) if(GetWorld()) \{\DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Red, true, -1.f, 0, 2.f);\DrawDebugPoint(GetWorld(), EndLocation, 15.f, FColor::Red, true);\}
#define DRAW_VECTOR_SingleFrame(StartLocation, EndLocation) if(GetWorld()) \{\DrawDebugLine(GetWorld(), StartLocation, EndLocation, FColor::Red, false, -1.f, 0, 2.f);\DrawDebugPoint(GetWorld(), EndLocation, 15.f, FColor::Red, false, -1.f);\}---------------------------------items.cpp-----------------------
void AItem::Tick(float DeltaTime)
{Super::Tick(DeltaTime);AddActorWorldOffset(FVector(1.f, 0.f, 0.f));DRAW_SPHERE_SingleFrame(GetActorLocation());
}
可以选择固定帧率
想要物体的移动速度,与在不同机器上的帧率无关,(例如每帧移动1cm,在120帧的高性能机器上,每秒移动120cm;而在30帧的机器上,则是每秒移动30cm。)可以使用delta time。让物体以每秒行进多少的单位恒定速度来移动
void AItem::Tick(float DeltaTime)
{Super::Tick(DeltaTime);//movement rate in units of cm/sfloat MovementRate = 50.f;float RotationRate = 45.f;// movementrate * deltatime = (cm/s) * (s/frame) = (cm/frame)AddActorWorldOffset(FVector(MovementRate * DeltaTime, 0.f, 0.f));AddActorWorldRotation(FRotator(0.f, RotationRate * DeltaTime, 0.f));DRAW_SPHERE_SingleFrame(GetActorLocation());DRAW_VECTOR_SingleFrame(GetActorLocation(), GetActorLocation() + GetActorForwardVector() * 100)
}
56、the sine function
c++成员变量的三种初始化方式
1、 在头文件中声明并初始化
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;private:float RunningTime;float Amplitude = 0.25f; //declare and initialize};
2、在头文件中声明,在构造函数中初始化
-----------items.h-------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;private:float RunningTime;float Amplitude; //declare
};
--------------------items.cpp------------------------
AItem::AItem()
{// 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;Amplitude = 0.25f; //在函数体里面赋值 被称为assignment;效率上比方法1和方法3低效
}
3、在头文件中声明,在构造函数内联初始化
----------------items.cpp-------------
AItem::AItem() : Amplitude(0.25f) //内联初始化,称为initialize or list
{// 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;
}
实现效果
-------------items.h--------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;private:float RunningTime;float Amplitude = 0.25f;float TimeConstant = 5.f;
};-------------items.cpp----------------
// Called every frame
void AItem::Tick(float DeltaTime)
{Super::Tick(DeltaTime);RunningTime += DeltaTime;// RunningTime * 5.f is speed upfloat DeltaZ = Amplitude * FMath::Sin(RunningTime * TimeConstant);AddActorWorldOffset(FVector(0.f, 0.f, DeltaZ));DRAW_SPHERE_SingleFrame(GetActorLocation());DRAW_VECTOR_SingleFrame(GetActorLocation(), GetActorLocation() + GetActorForwardVector() * 100)
}
57、exposing variables to blueprints
1、EditDefaultOnly(只能通过blueprint default panel编辑,在世界中的蓝图实例下无法查看该配置的变量)
Now there is a limitation of edit defaults only. 现在只有编辑默认值的限制。
We can only see and edit this property from the default blueprint. 我们只能从默认蓝图查看和编辑此属性。
We can edit it, but only in the default blueprint. 我们可以编辑它,但只能在默认蓝图中编辑它。
2、EditInstanceOnly(只能在世界实例上看到该属性,蓝图 详情默认面板无法查看)
3、EditAnywhere(蓝图默认detail panel和实例都可以编辑,但实例编辑的结构不会影响到蓝图;蓝图和实例的数值可能不统一)
---------------items.h---------------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public: // Called every framevirtual void Tick(float DeltaTime) override;private:float RunningTime;UPROPERTY(EditAnywhere)float Amplitude = 0.25f;UPROPERTY(EditInstanceOnly)float TimeConstant = 5.f;
};
官方文档:https://dev.epicgames.com/documentation/zh-cn/unreal-engine/unreal-engine-uproperties
58、visible but not editable
1、VisibleDefaultsOnly
只能在蓝图中查看,无法修改;在实例中没法查看。当tick函数是c++运行而不是蓝图运行时,该配置的属性不会更改数值,因为蓝图的tick函数没有运行
2、VisibleInstanceOnly
在实例中查看,蓝图中无法查看,在游戏运行时能通过detail面板查看
3、VisibleAnywhere
表示此属性在所有属性窗口中可见,但不可编辑。
59、exposing variables to event graph
Now, we've exposed some properties to Blueprint, but we've only exposed them to the details panel. 现在,我们已经向 Blueprint 公开了一些属性,但我们只将它们公开给了 details 面板。
This is great for changing values of these variables before the game starts, but if we want to change 这对于在游戏开始前更改这些变量的值非常有用,但如果我们想要更改
them at runtime, we have to change them here in the event graph. 它们,我们必须在 Event Graph 中更改它们。
1、BlueprintReadOnly
-----------items.h-----------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadOnly)float Amplitude = 0.25f;public: // Called every framevirtual void Tick(float DeltaTime) override;private:UPROPERTY(VisibleInstanceOnly)float RunningTime;UPROPERTY(EditInstanceOnly)float TimeConstant = 5.f;
};
不能放在private成员上
查看从c++脚本继承的变量
2、阻尼振荡
3、变量分组
-----------items.h-----------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "sine parameters")float RunningTime;public: // Called every framevirtual void Tick(float DeltaTime) override;private:UPROPERTY(EditInstanceOnly)float TimeConstant = 5.f;
};
4、meta
So meta allow private access equals true is what we have to add to the U property to expose a variable 所以 meta allow private access equals true 是我们必须添加到 U 属性中以公开变量的内容
that's private to the event graph. 这是事件图的私有属性。
--items.h-----------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float TimeConstant = 5.f;public: // Called every framevirtual void Tick(float DeltaTime) override;private://私有变量一般不暴露在event graph中,如果真的想让它课件,用meta//meta = (AllowPrivateAccess = "true") 元说明符 declare expose variables and keep variables privateUPROPERTY(VisibleAnywhere,BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))float RunningTime;
};
60、exposing function to blueprint
1、blueprintcallable
And blueprint callable makes a blue function with a blue F, it has an input execution pin and an output blueprint callable 生成一个带有蓝色 F 的蓝色函数,它有一个输入执行引脚和一个输出
------------items.h-------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();// Called every framevirtual void Tick(float DeltaTime) override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float TimeConstant = 5.f;UFUNCTION(BlueprintCallable)float TransformdSin(float Value);
private://私有变量一般不暴露在event graph中,如果真的想让它课件,用meta//meta = (AllowPrivateAccess = "true") 元说明符 declare expose variables and keep variables privateUPROPERTY(VisibleAnywhere,BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))float RunningTime;
};------------------items.cpp-------------
float AItem::TransformdSin(float Value)
{return Amplitude * FMath::Sin(Value * TimeConstant);;
}
2、blueprintpure
蓝图纯函数,没有引脚,显示为绿色
---------items.h-------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();// Called every framevirtual void Tick(float DeltaTime) override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float TimeConstant = 5.f;UFUNCTION(BlueprintPure)float TransformdSin();UFUNCTION(BlueprintPure)float TransformdCos();
private://私有变量一般不暴露在event graph中,如果真的想让它课件,用meta//meta = (AllowPrivateAccess = "true") 元说明符 declare expose variables and keep variables privateUPROPERTY(VisibleAnywhere,BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))float RunningTime;
};-------------items.cpp----------------
float AItem::TransformdSin()
{return Amplitude * FMath::Sin(RunningTime * TimeConstant);
}float AItem::TransformdCos()
{return Amplitude * FMath::Cos(RunningTime * TimeConstant);
}
61、template function
-----------items.h-------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();// Called every framevirtual void Tick(float DeltaTime) override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float TimeConstant = 5.f;UFUNCTION(BlueprintPure)float TransformdSin();UFUNCTION(BlueprintPure)float TransformdCos();template<typename T>T Avg(T First, T Second);
private://私有变量一般不暴露在event graph中,如果真的想让它课件,用meta//meta = (AllowPrivateAccess = "true") 元说明符 declare expose variables and keep variables privateUPROPERTY(VisibleAnywhere,BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))float RunningTime;
};template<typename T>
inline T AItem::Avg(T First, T Second)
{return (First + Second) / 2;
}-----------items.cpp-------------
void AItem::BeginPlay()
{Super::BeginPlay();int32 AvgInt = Avg<int32>(1, 3);UE_LOG(LogTemp, Warning, TEXT("Avg of 1 and 3:%d"), AvgInt);float AvgFloat = Avg<float>(3.45f, 7.86f);UE_LOG(LogTemp, Warning, TEXT("Avg of 3.45 and 7.86:%f"), AvgFloat);}float AItem::TransformdSin()
{return Amplitude * FMath::Sin(RunningTime * TimeConstant);
}float AItem::TransformdCos()
{return Amplitude * FMath::Cos(RunningTime * TimeConstant);
}// Called every frame
void AItem::Tick(float DeltaTime)
{Super::Tick(DeltaTime);RunningTime += DeltaTime;// RunningTime * 5.f is speed up//float DeltaZ = Amplitude * FMath::Sin(RunningTime * TimeConstant);//AddActorWorldOffset(FVector(0.f, 0.f, DeltaZ));DRAW_SPHERE_SingleFrame(GetActorLocation());DRAW_VECTOR_SingleFrame(GetActorLocation(), GetActorLocation() + GetActorForwardVector() * 100);FVector AvgVector = Avg<FVector>(GetActorLocation(), FVector::ZeroVector);DRAW_POINT_SingleFrame(AvgVector);//会报错 因为FRotator格式不支持/运算符// FRotator AvgRotator = Avg<FRotator>(GetActorRotation(), FRotator::ZeroRotator);}
62、components
63、components in c++
------------items.h----------------
UCLASS()
class MYPROJECT_API AItem : public AActor
{GENERATED_BODY()public: // Sets default values for this actor's propertiesAItem();// Called every framevirtual void Tick(float DeltaTime) override;protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float Amplitude = 0.25f;UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "sine parameters")float TimeConstant = 5.f;UFUNCTION(BlueprintPure)float TransformdSin();UFUNCTION(BlueprintPure)float TransformdCos();template<typename T>T Avg(T First, T Second);
private://私有变量一般不暴露在event graph中,如果真的想让它课件,用meta//meta = (AllowPrivateAccess = "true") 元说明符 declare expose variables and keep variables privateUPROPERTY(VisibleAnywhere,BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))float RunningTime;UPROPERTY(VisibleAnywhere)UStaticMeshComponent* ItemMesh;
};-------------items.cpp------------
AItem::AItem()
{// 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;ItemMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ItemMeshComponent"));RootComponent = ItemMesh;
}