一、引言
在游戏世界中有特别多的物品,NPC,怪物。使用UE4中的蓝图我们可以摆放这些事务,但是如何在指定范围内随机生成同一个种类的不同事物呢,这就需要UE4 实现 C++自定义 蓝图功能。
二、构思
首先我们构思一下实现上诉功能,应该怎么做。
1.想要在指定范围内,我们需要一个范围,这里我用 UBoxComponent,用于指定物品或怪物的生成范围;
2.生成的物品的种类是不同的,那么需要定义一个模板类对象;
3.我们需要随机生成物品,则可用数组,通过随机生成下标实现获取物品的效果。
4.最重要的来了:想要动态生成物品到世界中,光靠C++代码是很困难的,那么我们需要到蓝图中也去调用一下。也就是说C++函数实现一半功能,蓝图中调用该函数实现另外一半功能。
三、实现效果
如果需要拾取物蓝图,只需要在本蓝图的基础上复制,然后更改Actor即可。
这里提供怪物蓝图设置预览:
场景布置预览:
实际产出效果如下:
四、代码编写
头文件代码如下:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpawnVolume.generated.h"
UCLASS()
class MYFIRSTPROJECT_API ASpawnVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnVolume();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SpawningBox")
class UBoxComponent* SpawningBox;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spawning")
TSubclassOf
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spawning")
TSubclassOf
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spawning")
TSubclassOf
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spawning")
TSubclassOf
TArray
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintPure,Category = "Spawning")
FVector GetSpawnPonit();
UFUNCTION(BlueprintPure, Category = "Spawning")
TSubclassOf
UFUNCTION(BlueprintNativeEvent,BlueprintCallable,Category = "Spawning")
void SpawnOurActor(UClass* ToSpawn, const FVector& Location);
};
Cpp代码如下:
#include "SpawnVolume.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Engine/World.h"
#include "Critter.h"
#include "Enemy.h"
#include "AIController.h"
// Sets default values
ASpawnVolume::ASpawnVolume()
{
// 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;
SpawningBox = CreateDefaultSubobject
}
// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();
if (Actor_1) {
SpawnArray.Add(Actor_1);
}
if (Actor_2) {
SpawnArray.Add(Actor_2);
}
if (Actor_3) {
SpawnArray.Add(Actor_3);
}
if (Actor_4) {
SpawnArray.Add(Actor_4);
}
}
// Called every frame
void ASpawnVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
FVector ASpawnVolume::GetSpawnPonit()
{
FVector Extent = SpawningBox->GetScaledBoxExtent();
FVector Origin = SpawningBox->GetComponentLocation();
FVector Point = UKismetMathLibrary::RandomPointInBoundingBox(Origin,Extent);
return Point;
}
void ASpawnVolume::SpawnOurActor_Implementation(UClass* ToSpawn,const FVector& Location)
{
if (ToSpawn)
{
UWorld* World = GetWorld();
FActorSpawnParameters SpawnParams;
if (World)
{
AActor* Actor = World->SpawnActor
AEnemy* Enemy = Cast
if (Enemy) {
//生成默认AI控制器
Enemy->SpawnDefaultController();
AAIController* AICont = Cast
if (AICont) {
Enemy->AIController = AICont;
}
}
}
}
}
TSubclassOf
if (SpawnArray.Num() > 0) {
int32 Selection = FMath::RandRange(0, SpawnArray.Num() - 1);
return SpawnArray[Selection];
}
else {
return nullptr;
}
}
4.蓝图调用: