UE4 C++ 的SpawnActor

UE4 C++ 的SpawnActor

// 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(BlueprintPure, Category = "Spawn Volume")
	FVector GetSpawnPoint();

	UFUNCTION(BlueprintPure, Category = "Spawn Volume")
	TSubclassOf<AActor> GetSpwnActorClass();


	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Spawn Volume")//有了BlueprintNativeEvent后我们的反射系统就会创建出两个东西
	void SpawnActor(UClass* SpawnClass,FVector SpawnLocation);

};

// 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(UClass* SpawnClass, FVector SpawnLocation)//这个就像是SpawnActor的父类的版本
{
	if (SpawnClass) {
		GetWorld()->SpawnActor<AActor>(SpawnClass, SpawnLocation, FRotator(0.0f));
	}

}


用蓝图调用C++写好的代码
在这里插入图片描述

给公开的变量集合赋值
在这里插入图片描述

运行后:
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h>    参数化输入/输出 #include <iostream h>    数据流输入/输出 #include <limits h>     定义各种数据类型最值常量 #include <locale h>     定义本地化函数 #include <math h>      定义数学函数 #include <stdio h>     定义输入/输出函数 #include <stdlib h>     定义杂项函数及内存分配函数 #include <string h>     字符串处理 #include <strstrea h>    基于数组的输入/输出 #include <time h>      定义关于时间的函数 #include <wchar h>     宽字符处理及输入/输出 #include <wctype h>     宽字符分类 int spawnvpe int mode char pathname char argv[] char envp[] spawn函数族在mode模式下运行子程序pathname 并将参数 arg0 arg1 arg2 argv[] envp[] 传递给子程序 出错返回 1 mode为运行模式 mode为 P WAIT 表示在子程序运行完后返回本程序 P NOWAIT 表示在子程序运行时同时运行本程序 不可用 P OVERLAY表示在本程序退出后运行子程序 在spawn函数族中 后缀l v p e添加到spawn后 所指定的函数将具有某种操作能力 有后缀 p时 函数利用DOS的PATH查找子程序文件 l时 函数传递的参数个数固定 v时 函数传递的参数个数不固定 ">#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h& [更多]
以下是一个简单的UE4 C++生成Actor的例子,假设你已经创建了一个新的C++项目: 1. 创建一个新的C++类,继承自AActor。例如,我们创建一个名为MyActor的类: ```cpp UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; }; ``` 2. 在MyActor.cpp文件中实现构造函数和BeginPlay函数: ```cpp AMyActor::AMyActor() { // 设置Actor的根组件 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent")); // 创建一个静态网格组件 UStaticMeshComponent* MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent")); MeshComponent->SetupAttachment(RootComponent); // 加载静态网格 constructorHelpers::FObjectFinder<UStaticMesh> Mesh(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube")); if (Mesh.Succeeded()) { MeshComponent->SetStaticMesh(Mesh.Object); } } void AMyActor::BeginPlay() { Super::BeginPlay(); // 在控制台输出Actor的位置 UE_LOG(LogTemp, Warning, TEXT("MyActor is at %s"), *GetActorLocation().ToString()); } ``` 3. 在MyActor.cpp文件中实现Tick函数,使Actor在每帧旋转: ```cpp void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // 每帧旋转Actor FRotator Rotation = GetActorRotation(); Rotation.Yaw += DeltaTime * 100.f; SetActorRotation(Rotation); } ``` 4. 在MyActor.h文件中添加头文件和构造函数声明: ```cpp #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "Engine/StaticMesh.h" #include "Components/StaticMeshComponent.h" #include "MyActor.generated.h" UCLASS() class MYPROJECT_API AMyActor : public AActor { GENERATED_BODY() public: AMyActor(); virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; private: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true")) UStaticMeshComponent* MeshComponent; }; ``` 5. 在你的场景中添加一个MyActor,例如在GameMode的BeginPlay函数中: ```cpp void AMyProjectGameModeBase::BeginPlay() { Super::BeginPlay(); // 创建一个新的MyActor AMyActor* MyActor = GetWorld()->SpawnActor<AMyActor>(AMyActor::StaticClass(), FVector(0.f, 0.f, 0.f), FRotator(0.f, 0.f, 0.f)); } ``` 6. 运行游戏,你应该可以看到一个旋转的立方体Actor。 希望这个例子能够帮助你理解如何使用UE4 C++生成Actor!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Li~蒙一

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

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

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

打赏作者

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

抵扣说明:

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

余额充值