UE4 C++的函数和事件的定义
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpwanVolume.generated.h"
UCLASS()
class GETSTARTED_API ASpwanVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpwanVolume();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
class UBoxComponent* SpawnBox;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spawn Volume")
TArray<TSubclassOf<AActor>> SpawnActorClassesArray;//TSubclassOf 一个模板类
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "Spawn Volume")
FVector GetSpawnPoint();
UFUNCTION(BlueprintCallable, Category = "Spawn Volume")
TSubclassOf<AActor> GetSpwnActorClass();
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Spawn Volume")
void SpawnActor();
};
```cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Gameplay/SpwanVolume.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
// Sets default values
ASpwanVolume::ASpwanVolume()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SpawnBox = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnBox"));
RootComponent = SpawnBox;
}
// Called when the game starts or when spawned
void ASpwanVolume::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASpwanVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
FVector ASpwanVolume::GetSpawnPoint()
{
const FVector Origin = SpawnBox->GetComponentLocation();
const FVector Extent = SpawnBox->GetScaledBoxExtent();
return UKismetMathLibrary::RandomPointInBoundingBox(Origin, Extent);
}
TSubclassOf<AActor> ASpwanVolume::GetSpwnActorClass()
{
if (SpawnActorClassesArray.Num() > 0) {
int index = FMath::RandRange(0, SpawnActorClassesArray.Num() - 1);
return SpawnActorClassesArray[index];
}
else {
return nullptr;
}
}
void ASpwanVolume::SpawnActor_Implementation()//这个就像是SpawnActor的父类的版本
{
}