Download

How to spawn dynamic actor without using ConstructorHelpers

Dear Unreal Community.

I’m utterly confused… We have a need to spawn dynamic objects at run time from Blueprints (with path). However, we do not know before hand what object or what path need to be loaded. Hence we cannot pre-load it with ConstructorHelpers since it’s only allowed at Constructor. (We have too many objects to pre-loaded anyway…)

So, how exactly do you spawn dynamic actor from a file path? I can devise some round-about way to do it, but I’m sure there must be some correct/easier way to do this though…

My thanks in advance.

“So, how exactly do you spawn dynamic actor from a file path?”


**Dynamic Load Object**

I have written functions for you as follows

1. how to get the path of any object 

2. how to load an object from the path in #1

**wiki link**
https://wiki.unrealengine.com/Dynamic_Load_Object

Blueprints

to do this in blueprints, write a Blueprint Node Library function that does what I posted for you above

Blueprint Node Library wiki

Rama

My apology for double post earlier. Must have accidentally created another one when I editted the subject name.

all good, it happens :slight_smile:

if you have any questions about my 2 wiki tutorials related to this topic make sure to post some of your code as well

:slight_smile:

Rama

You can also spawn an object from a class name:



FString className("MyActorClass");
UClass* classPtr = FindObject<UClass>(ANY_PACKAGE, *className);
if (classPtr) {
		AActor* obj = world->SpawnActorDeferred<AActor>(classPtr, FVector::ZeroVector, FVector::ZeroVector.Rotation(), NULL, NULL, false);
		obj->OnConstruction(obj->GetTransform());
		obj->PostActorConstruction();
}


If you have the engine source, take a look at Engine\Source\Runtime\Engine\Private\GameplayStatics.h and .cpp, they contain a lot of the code called by blueprints. A treasure trove of examples, really.

EDIT: and of course you can spawn the object with the proper class if you know it already instead of AActor…

Dear RAMA:

Wow, ty. So I’ll try using StaticLoadObject instead. However, since Unreal have re-indexed all of its reference pages on the web, I couldn’t really go read its description.
Can you help explain just briefly the differences betweeen [StaticLoadObject()] and [ConstructorHelpers::FObjectFinder]?

Dear dmacesic:

We have only one custom class, however, they are saved as blueprints into different objects. So I’m not sure if I can load them via class name (plz, correct me if I’m wrong).
[World->SpawnActorDeferred] and [obj->OnConstruction] is truely something helpful that I haven’t known about though, so ty!

ps. Thxz again for all your wonderful tutorials, Rama. I know tons of people really appreciate your work here. :slight_smile:

Hee hee!

StaticLoadObject let’s you load a value any time, outside of the constructor function, you should use the constructorhelper only inside the constructor function.

But you can use StaticLoadObject anywhere :slight_smile:

Enjoy!

Rama

Yeah… so I don’t get it… why do we even bother with the ConstructorHelpers? StaticLoadObject() seems to be superior in every way. I’ve tested it and it works beautifully.

Is there some hidden cost that I should be aware about?

Thx though, consider this issue solved.

You could call StaticLoadObject instead, but ConstructorHelpers cache the pointer to the asset, so there is zero overhead each time you create your object, whereas StaticLoadObject does have an overhead where it searches for the object based on its name.

Also note that when you load objects by name, the packager won’t automatically know to cook/package those objects, so you will have to manually add them to the list in the ini file.

JamesG has an extremely important point!

If your assets are not references anywhere when you cook your project, and are only loaded via StaticLoadObject, they wont get cooked and wont be found in packaged game!

So make sure to do what JamesG suggests and add them to your .ini file :slight_smile:

Rama

Ok, gotcha.

Thxz a tons. :slight_smile: