That worked perfectly for me, thanks a lot.
Here’s s copy&paste version for anyone:
TSubclassOf<class UObject> Common::FindOrLoadBluePrintClass(const TCHAR* path)
{
// finds or loads a blueprint class. Needed to create BP based objects in C++.
/*
// usage:
TSubclassOf<class UObject> BP_Item3DActor= FindOrLoadBluePrintClass(TEXT("/Game/FirstPerson/Blueprints/BP_Item3DActor"));
AItem3DActor* t = Common::world->SpawnActor<AItem3DActor>(BP_Item3DActor, wPos, SpawnRotation, SpawnParams);
// 't' actually points to a BP class derived from C++ class 'AItem3DActor'
*/
UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, path);
UBlueprint* bp = Cast<UBlueprint>(something);
TSubclassOf<class UObject> MyItemBlueprint;
MyItemBlueprint = (UClass*)bp->GeneratedClass;
return MyItemBlueprint;
}
I had the same issue and struggled a bit to get the solution from the first answer to work from blueprints - so for everyone who has the same problem. First of all i created a c++ blueprintfunction library, here the header:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"
UCLASS()
class PICKDROPPROTOTYP_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, BlueprintPure, Category="SunShine")
static TSubclassOf<class UObject> FindOrLoadBluePrintClass(FString path);
};
Here the cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "PickDropPrototyp.h"
#include "MyBlueprintFunctionLibrary.h"
TSubclassOf<class UObject> UMyBlueprintFunctionLibrary::FindOrLoadBluePrintClass(FString path) //const TCHAR* path)
{
TSubclassOf<class UObject> MyItemBlueprint = NULL;
UObject* something = StaticLoadObject(UObject::StaticClass(), nullptr, *path);
if(something != NULL)
{
UBlueprint* bp = Cast<UBlueprint>(something);
if(bp != NULL)
{
if(GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("found some ■■■■")));
MyItemBlueprint = (UClass*)bp->GeneratedClass;
}
else
{
if(GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("bp is NULL")));
}
}
else
{
if(GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("something is NULL")));
}
return MyItemBlueprint;
}
And here a BP example how to use it (the path you can just get by the content browser, right-click on some StaticMeshActor and select “Copy Reference”)
In case somebody is looking for this. If you need the “Reference” to an object, you can just find it in your UEditor browser, right click, COPY REFERENCE → It will return something that you can paste in your code: example: Blueprint’/Game/Blueprints/BPPortal.BPPortal’