目录
UE4 C++编程官方文档示例代码“缺省”补充
(边学边更新)
本来是想做勘误,但发现示例代码的问题大部分都是缺头文件(这应该和现在自建的类包含的都是CoreMinimal.h,而示例代码都是自建类包含Engine.h时期写的)以及少数函数版本过时的问题,所以本博客可就总结一些常用类对应的头文件和过期函数的替代。
常用类的头文件
UCameraComponent
“Camera/CameraComponent.h”
USpringArmComponent
“GameFrameWork/SpringArmComponent.h”
USphereComponent
“Components/SphereComponent.h”
UParticleSystemComponent
“Particles/ParticleSystemComponent.h”
UUserWidget
“Blueprint/UserWidget.h”
弃用函数
AttachTo
替代1:AttachToComponent
函数声明如下:
void AttachToComponent
(
USceneComponent * Parent,
const FAttachmentTransformRules & AttachmentRules,
FName SocketName
)
丢一段使用示例:
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.f);
SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
UStaticMeshComponent* SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Pawn"));
/*******************************************************************************************/
SphereVisual->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform, TEXT("RootComponent"));
/*******************************************************************************************/
static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
if (SphereVisualAsset.Succeeded()) {
SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
SphereVisual->SetRelativeLocation(FVector(0.f, 0.f, -40.f));
SphereVisual->SetWorldScale3D(FVector(0.8f));
}
//创建粒子系统
OurParticleSystem = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MovementParticles"));
/*******************************************************************************************/
OurParticleSystem->AttachToComponent(SphereVisual, FAttachmentTransformRules::KeepRelativeTransform, TEXT("Pawn"));
/*******************************************************************************************/
OurParticleSystem->bAutoActivate = true;
OurParticleSystem->SetRelativeLocation(FVector(-20.f, 0.f, -20.f));
static ConstructorHelpers::FObjectFinder<UParticleSystem>ParticleAsset(TEXT("/Game/StarterContent/Particles/P_Fire.P_Fire"));
if(ParticleAsset.Succeeded()) {
OurParticleSystem->SetTemplate(ParticleAsset.Object);
}
替代2:SetupAttachment
上一个是输出日志的warning推荐的替代方式,这个是在官方的蓝图转c++学到的,会比上一个更简单一点。
SphereVisual->SetupAttachment(RootComponent);