在PickUP中 添加WasCollected 函数 他的属性为BlueprindNativeEvent 是优先调用蓝图中的该函数 如果蓝图中没有单独实现 则调用+_Implementation()函数
最终PickUp.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; }
UFUNCTION(BlueprintNativeEvent)
void WasCollected(); //蓝图中调用
virtual void WasCollected_Implementation(); //蓝图没有实现WasCollected的话 调用c++中的实现
protected:
bool bIsActive;
private:
UPROPERTY(VisibleAnyWhere, BlueprintReadOnly, Category = "Pickup", meta=(AllowPrivateAccess = "true"))
UStaticMeshComponent* PickupMesh;
};
最终PickUp.cpp
// Fill out your copyright notice in the Description page of Project Settings.
include "BatteryCollector.h"
include "PickUp.h"
// Sets default values
APickUp::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<UStaticMeshComponent>(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;
}
void APickUp::WasCollected_Implementation()
{
FString PickupDebugString = GetName();
UE_LOG(LogClass, Log, TEXT("You have collected %s"), *PickupDebugString);
}
为PickUp的子类重载该函数
BatteryPickUp.h
打开系统生成的BatteryCollectorCharacter 为其添加SphereCompoment
构造函数中进行创建 添加