x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Spawning an actor from just the path in c++

I've seen a bunch of similar questions but every one has a different or slightly-outdated answer. I know I can use a list of class references to do this without strings, but humour me.

The following works, but only in the editor. In a win64 build, the checkf fails:

 static const FSoftObjectPath BlockPath(TEXT("Blueprint'/Game/Cas/BuildingBlock.BuildingBlock'"));
 const auto BlockObject = UAssetManager::GetStreamableManager().LoadSynchronous(BlockPath);
 checkf(BlockObject != nullptr, TEXT("expected non-null class"));
 const auto& BlockClass = Cast<UBlueprint>(BlockObject)->GeneratedClass;
 GetWorld()->SpawnActor(BlockClass, &SpawnPos);

I've added the Cas directory to "Additional Asset Directories to Cook" in the project's Packaging settings, but I know this block is already used in the level anyway.

I've also tried using my own FStreamableManager, using ResolveObject() on the FSoftObjectPath, and using EngineUtils::FindOrLoadAssetsByPath to similar non-effects.

I've also tried with this approach, which also fails in builds but works in the editor:

 const auto BlockObject = Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), nullptr, TEXT("/Game/Cas/BuildingBlock")));
 checkf(BlockObject != nullptr, TEXT("expected non-null class"));
 const auto& BlockClass = BlockObject->GeneratedClass;

...which is weird because I've used that elsewhere in the codebase to load textures.

What else can I try? What more info can I give you?

Product Version: UE 4.20
Tags:
more ▼

asked Jan 03 '19 at 11:09 PM in C++ Programming

avatar image

tenpn
3 1 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

For a blueprint you'd want to use FSoftClassPath instead of FSoftObjectPath, and you then need to append "_C" to the end of your object's path. You can then use FSoftClassPath::TryLoadClass to load and get your UClass pointer, which you can pass to SpawnActor

So maybe something like this, I think:

 const FSoftClassPath BlockPath(TEXT("/Game/Cas/BuildingBlock.BuildingBlock_C"));
 const UClass* BlockClass = BlockPath.TryLoadClass();
 checkf(BlockClass != nullptr, TEXT("expected non-null class"));
 GetWorld()->SpawnActor(BlockClass, &SpawnPos);
more ▼

answered Jan 04 '19 at 11:21 AM

avatar image

tomvernon
91 2 5 6

avatar image tenpn Jan 04 '19 at 11:23 AM

TryLoadClass needs a template param, and UObject worked for me. now it loads everywhere!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question