UE4 官方文档C++编程教程笔记

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值