04 - Adding Variables & Functions

继续完善PickUP类

直接上图


04 - Adding Variables & Functions_第1张图片

其中需要注意的是IsActive函数设置的蓝图属性是BlueprintPure 表明该函数只是读取数据 不做修改类似C#中的get域

最终。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;

UFUNCTION(BlueprintPure, Category = "Pickup")

bool IsActive();

UFUNCTION(BlueprintCallable, Category = "Pickup")

void SetActive(bool NewPickupState);

public:

// Called every frame

virtual void Tick(float DeltaTime) override;

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

protected:

bool bIsActive;

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;

bIsActive = true;

}

// Called when the game starts or when spawned

void APickUp::BeginPlay()

{

Super::BeginPlay();

}

// Called every frame

void APickUp::Tick(float DeltaTime)

{

Super::Tick(DeltaTime);

}

bool APickUp::IsActive()

{

return bIsActive;

}

void APickUp::SetActive(bool NewPickupState)

{

this->bIsActive = NewPickupState;

}

进入UE4 找到PickUP 右键 创建子类蓝图


04 - Adding Variables & Functions_第2张图片

你可能感兴趣的:(04 - Adding Variables & Functions)