Packaged game can't spawn actor from plugin using StringAssetReference

Hi,

I try to spawn a Blueprint actor from a string reference such as:
‘/MyPlugin/BP/BP_Helicopter.BP_Helicopter’

The Blueprint and assets are in a Plugin.

It works in the editor.
But not in the packaged game.

TryLoad() failed:

FString blueprintRefString = "'/MyPlugin/BP/BP_Helicopter.BP_Helicopter'";
FStringAssetReference itemRef = blueprintRefString;
if (itemRef.TryLoad() != NULL) // Fail
{
	// ... Spawn ...
}

I have also tried:
‘Class/MyPlugin/BP/BP_Helicopter.BP_Helicopter_C’
Same result.

I need to load Blueprints asset listed in a xml file.
How can i solve the problem ?

Note 1: This same Blueprint is displayed in the Packaged game if i drag and drop it in the scene.
Note 2: String Asset References work when my asset is not in a plugin but in the project itself.

loadPlug.zip (11.1 MB)

Recreated your plugin structure + asset path

use ConstructorHelpers::FClassFinder to get the class and spawn the actor on begin play

header .h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Loader.generated.h"

UCLASS()
class LOADPLUG_API ALoader : public AActor
{
	GENERATED_BODY()
	
public:	

	ALoader();

protected:

	virtual void BeginPlay() override;

public:	

	virtual void Tick(float DeltaTime) override;

	bool bSpawn = false;

	UClass* ActorClass = nullptr;

};

.cpp

#include "Loader.h"

// Sets default values
ALoader::ALoader()
{

	PrimaryActorTick.bCanEverTick = true;

	bSpawn = false;

	static ConstructorHelpers::FClassFinder<AActor>actorC(TEXT("/MyPlugin/BP/BP_Helicopter.BP_Helicopter_C"));
	ActorClass = actorC.Class;

	if (actorC.Succeeded()) {
		bSpawn = true;		
	}

}

void ALoader::BeginPlay()
{
	Super::BeginPlay();
	
	if (bSpawn == true && ActorClass != nullptr) {
		FActorSpawnParameters sp;
		sp.bNoFail = true;
		FTransform t;
		AActor* act = GetWorld()->SpawnActor<AActor>(ActorClass, t, sp);
	}	
}

void ALoader::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

Object loads from plugin content path and spawns the blueprint at 0,0,0

1 Like

Thanks for your replay.

But ConstructorHelpers need to be called in constructor.
I have a big list of actors which can be added or not dynamically in the game.

If i use your solution, i will need to use “ConstructorHelpers::FClassFinder” for every assets, even if there are not used in my game.

Is there a more “dynamic” solution ?

Does FClassFinder create a instance of the Blueprint object ? The documentation is not clear (empty).