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"

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++.

Product Version: UE 4.9
more ▼

asked Oct 17 '15 at 12:52 PM in C++ Programming

avatar image

200 54 54 64

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

2 answers: sort voted first

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:

Class holder

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

My custom BP

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.

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.

Always cook

more ▼

answered Oct 22 '15 at 02:44 PM

avatar image

6.4k 127 98 607

avatar image Allar Oct 22 '15 at 02:49 PM
avatar image Solicio Oct 22 '15 at 08:06 PM

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?

avatar image Moss Oct 23 '15 at 07:31 AM

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 ^^).

avatar image Solicio Oct 23 '15 at 07:32 AM

Cool, looking forward to it! :D

avatar image Moss Oct 26 '15 at 05:47 PM

Did the solution work for you?

avatar image Solicio Oct 26 '15 at 05:51 PM

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

avatar image Moss Oct 26 '15 at 06:05 PM

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

avatar image Solicio Oct 27 '15 at 08:08 AM

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.

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

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.

more ▼

answered Oct 23 '15 at 03:35 AM

avatar image

91 14 14 22

(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