03 - Making Your First Pickup Class

点击File/New C++ class 创建新的c++类 继承自Actor 命名为PickUp


03 - Making Your First Pickup Class_第1张图片

添加私有变量PickupMesh 类型为网格指针 其中设置UPROPERTY 第一个参数表明在蓝图编辑器中可见 第二个参数表明在蓝图中为只读 第三个参数是元数据 表示该变量可被私有访问(因为在蓝图中可以被读取 所以需要设置该元数据)


03 - Making Your First Pickup Class_第2张图片

添加函数用来获取该mesh


03 - Making Your First Pickup Class_第3张图片

在Actor构造函数中通过CreateDefaultSubobject 创建Component 并设置为Root节点


03 - Making Your First Pickup Class_第4张图片

最终。h文件

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

#pragma once

#include "GameFramework/Actor.h"

#include "PickUp.generated.h"

UCLASS()

class BATTERYCOLLECTOR_API APickUp : public AActor

{

GENERATED_BODY()

public:

// Sets default values for this actor's properties

APickUp();

protected:

// Called when the game starts or when spawned

virtual void BeginPlay() override;

public:

// Called every frame

virtual void Tick(float DeltaTime) override;

FORCEINLINE class UStaticMeshComponent* GetMesh()const { return PickupMesh; }

private:

UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "Pickup", meta=(AllowPrivateAccess = "true"))

UStaticMeshComponent* PickupMesh;

};

最终。cpp文件

// Fill out your copyright notice in the Description page of Project Settings.#include "BatteryCollector.h"#include "PickUp.h"// Sets default valuesAPickUp::APickUp(){ // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = false;PickupMesh = CreateDefaultSubobject(TEXT("PickupMesh"));

RootComponent = PickupMesh;

}

// Called when the game starts or when spawned

void APickUp::BeginPlay()

{

Super::BeginPlay();

}

// Called every frame

void APickUp::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);

}

你可能感兴趣的:(03 - Making Your First Pickup Class)