Need help spawning a Blueprint that inherits from a C++ UObject class

Hey, i’m having some trouble spawning a blueprint from my C++ code and would appreciate some help with it.

This is the base C++ class of the blueprint i wish to spawn. I have created a blueprint that inherits from this class.

class HOMEINVASION_API UMurdererWaypointManager : public UObject

	UMurdererWaypointManager(const FObjectInitializer& ObjectInitializer);

	/** Find as all the waypoints in the current level. */
	UFUNCTION(BlueprintCallable, Category = "Waypoint Functions")
	void FindWaypoints();


But i have gotten stuck at this next part, in my GameMode i try to spawn the blueprint but cannot figure out how i should do this. I can spawn the class just fine if i just spawn the C++ version of the class using this.

WaypointManager = NewNamedObject<UMurdererWaypointManager>(this, "WaypointManager");

I tried using this to spawn the blueprint, but it did not work. (I check if the WaypointManager is valid)

In Gamemode.h

UMurdererWaypointManager* WaypointManager;

TSubclassOf<class UMurdererWaypointManager> WaypointManagerClass;

In Gamemode.cpp

static ConstructorHelpers::FObjectFinder<UBlueprint> WaypointManagerBP(TEXT("/Game/MurdererAI/AI_Blueprints/BP_MurdererWaypointManager"));
if (WaypointManagerBP.Object)
	WaypointManagerClass = (UClass*)WaypointManagerBP.Object->GeneratedClass;

if (GetWorld())
	GetWorld()->SpawnActor<UMurdererWaypointManager>(WaypointManagerClass, FVector::ZeroVector, FRotator::ZeroRotator);

Would be great if someone could help me out by telling me how you go about spawning blueprints that inherit from UObject in c++.

SpawnActor is used to spawn an actor, UObject’s are not actors they are objects. To instantiate an UObject you should use NewObject and hold the instance in a property field of some of your other classes.

UMyClass* myInstance= NewObject<UMyClass>();

Now that you know how to create a new object let’s check on spawning a Blueprint class. The first thing we have to clear out is to outline that a Blueprint is an asset, it’s content and content has some special treating when creating a packaged game. All content in your project get’s cooked, cooking is just a process of ripping of some editor specific data from your assets and prepare it for faster loading (on optical media content should be aligned by 16 bits to lower seek times for example). So what the engines does is to scan you project and to cook only content that is actually referenced so that you package only the stuff you are actually using. That is actually nice and useful but it comes with an issue: when you are running in the editor you have uncooked data and access to everything so many people fail to package their game because their stuff is not available when they create a release of their project.

To solve the cooking Odyssey you have to simply ensure your content is referenced where you need it. We will show a simple way to reference your Blueprints based on the Actor class (you can adapt it for your own base classes though).

Add a property to your C++ class to hold a class of your BP.

/** The class of Object to instantiate, use your own base C++ */
UPROPERTY(EditAnywhere, noclear, BlueprintReadOnly, Category = Spawning)
TSubclassOf<class UObject> ObjectClass;

I added the snippet above into a child of AGameMode and your should got something like this:

Then I just created a new BP that inherits from UObject directly and selected it from the drop down:

The next step is to use that property to spawn a new

We just use our property to create a new object within the Transient package, this is important when creating objects in run-time to respect the GCs refernce chain.

UObject* NewBPClass = NewObject<UObject>(GetTransientPackage(), ObjectClass);

But I do not want to reference every thing always

Now that you know the basics I’ll post some references to get you started referencing you assets painlessly and to load them in blocking and non-blocking fashion.

How to cook something always

To be able to load a class being it blocking or non-blocking the content needs to be cooked forehand. You could for example just create some directories that will get cooked always and store your dynamic content in those folders.

For more details about Object Creation, go here: UObject Instance Creation | Unreal Engine Documentation

Yeah, i got the NewObject() function to work, but i cannot figure out how to make it create a new object of a blueprint class that inherits from a UObject class.

Lets say I have a C++ class called UMyObject and i create a blueprint that has this C++ class as it’s parent and call it BP_MyObject. How would i go about creating a new object using BP_MyObject?

I guess what you want is create a class from a blueprint that you created in the editor?

I’ve managed this way:

static ConstructorHelpers::FClassFinder<APawn> BotPawnOb(TEXT("/Game/Blueprints/AI/BotPawn"));
DefaultBotClass = BotPawnOb.Class;

where /Game/ is your Content directory, I suppose.

Cool, looking forward to it! :smiley:

I’ll update my post with a full example of how to reference a BP class within C++ safely that will work also in a cooked build. I’ll post the code once I’m back home (on mobile currently ^^).

Did the solution work for you?

Have not had the time to check it yet, think i have time tomorrow and will give it a try.

Nice ^^ if you have any questions feel free to ask ^^

Hey, i was able to give it a try and it works perfectly! Thank you!
The links were also an interesting read, will have to save those for later reference.