[UE4]C++中SpawnActor用法(动态创建Actor)
[UE4]C++中SpawnActor用法(动态创建Actor)
C++中创建一个Level并添加的Runtime当中
Level.Add(GetWorld()->SpawnActor<ABuildingModLevel>());
C++中Spawn一个基于蓝图的Actor
https://answers.unrealengine.com/questions/60897/spawn-actorobject-from-code.htm
Here is how I spawn a blueprint via C++. Note that the blueprint I spawn has a base class that was created in C++ also.
.h
TSubclassOf BlueprintVar; // YourClass is the base class that your blueprint uses
.cpp(注意,这段代码必须放在构造函数中。UE4其他类型的蓝图,比如Widget蓝图,都可以通过下面这种方式加载。)
复制代码
ClassThatWillSpawnTheBlueprint::ClassThatWillSpawnTheBlueprint(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) { static ConstructorHelpers::FObjectFinder<UBlueprint> PutNameHere(TEXT("Blueprint'/Path/To/Your/Blueprint/BP.BP'")); if (PutNameHere.Object) { BlueprintVar = (UClass*)PutNameHere.Object->GeneratedClass; } }
复制代码
PutNameHere is just an arbitrary name you give to the constructor helper. The path to your blueprint is found by finding your blueprint in the content browser, right clicking it, and choosing Copy Reference. Then, just paste that in between the quotes.
Now, you’re ready to spawn the blueprint. You can do it in BeginPlay() or wherever, just not in the constructor.(这段代码必须放在非构造函数中,比如BeginPlay()中)
UWorld* const World = GetWorld(); // get a reference to the world if (World) { // if world exists YourClass* YC = World->SpawnActor<YourClass>(BlueprintVar, SpawnLocation, SpawnRotation); }
If you don’t know your SpawnLocation or SpawnRotation you can just throw in FVector(0,0,0) and FRotator(0,0,0) instead.
So, since your blueprint base class was also created in C++ this makes it easy to interact with your blueprint from code. It’s as simple as YC->SomeVariable = SomeValue. Hope that helps.
APuzzleBlock* NewBlock = GetWorld()->SpawnActor(BlockLocation, FRotator(0,0,0));
- 点赞
- 收藏
- 分享
- 文章举报
- [UE4]C++ string的用法和例子
- [UE4]C++创建对象的三种方式
- [UE4]C++三种继承方式
- [UE4]C++的const类成员函数
- [UE4]C++ getter and setter
- [UE4]C++ 动态内存分配的6种情况
- Josephus问题,数组和链表(C++实现)
- c语言之逗号运算符
- C语言之运算符优先级
- C++多态及其实现原理
- C++一个类对象的大小计算
- 顺序线性表的存储与实现(C语言版)
- C语言总结
- Closest Pair 解题小记:C++运行错误:process returned -1073741571 且 函数计算错误
- C语言之函数的参数传递
- C语言之结构体中“ . ”和“ -> ”的区别
- C语言之双向链表
- C++异常处理机制
- C++调用Pytorch的坑:遇到free(): invalid pointer:
- 《计算方法与实习》 c语言与python分别实现二分法求方程的根