UE4 C++事件的定义

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的父类的版本
{
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li~蒙一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值