Spawn Blueprint from C++

Hello all,

I am trying to spawn a Blueprinted object in my game and I cant seem to get this to work.

I have “Items” that are partially code and partially Blueprint. When they are added to the player inventory system, they need to pass the Path to the Blueprint or something to the inventory manager that will be maintained. I then destroy the actual Item Actor. I spawn them again when they are dropped or used (removed from inventory).

This system is dynamic, I pick up items, drop them, etc.

I have the framework coded for the inventory management and now I am trying to figure out what to give it so that I can spawn the Item from the Item Blueprint.

I saw a few other questions like this, specifically THIS one but I am still unable to get this to work.

Any advice would be very helpful.

Thanks.

Hi ,

Yeah, I did this with just the AItem class… all in C++ except for the Mesh Component. I assign the Mesh in the Blueprint. That is where this problem came from. I kept the StaticClass() in the Inventory and then when the player drops the AItem from inventory I spawn it again but it had no Mesh because it was assigned in the Blueprint.

So you think I should just assign the Item StaicMeshComponent in C++ and then just do it with the StaticClass() passing and spawning?

Kind of like answered HERE?

You can’t, you would need to get asset but it no quite efficient of doing this

Create base class “AItem” in C++, implement all things you need for a item and make you inventory satisfied just with Item, then make classes based of AItem class which have code for actual items. Now i assume you made inventory system which use pickup and inventory system sepretly (which is something you don’t need to do you can simply add and remove mesh component), you could store UClass objects of classes (you can get i via GetClass() or StaticClass()) and span object in C++ via ()->SpawnActor(UClass);

did you try GetClass()?

Well, I just surprised myself. I guess I got the break I needed while waiting for help because I got it to work perfect.

Here’s how for anyone else that comes across this:

I have the majority of my Item class in C++ with a few things in a Blueprint. In the editor I drop the Blueprinted Item all over the map for my player to pick up. When I interact with the item, my inventory system stores the class, in my case the Blueprinted class, of the particular item that I picked up in my inventory (along with other info for book keeping) and destroys the actor in game. When my player equips or drops the item, I simply spawn the Blueprint Actor into the game and viola, new Actor from Blueprint spawned from C++ dynamically.

In my Item class header file I made a pointer to hold the Blueprinted class name:

AItem.h

TSubclassOf<class AItem> MyItemBlueprint;

Then in the Constructor for the AItem I grab the class of the blueprint and assign it to the MyItemBlueprint variable:

AItem.cpp

AItem::AItem(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

	// do things

...

	static ConstructorHelpers::FObjectFinder<UBlueprint> ItemBlueprint(TEXT("Blueprint'/Game/Items/Blueprints/BP_ItemTest.BP_ItemTest'"));
	if (ItemBlueprint.Object){
		MyItemBlueprint = (UClass*)ItemBlueprint.Object->GeneratedClass;
	}

}

Then, when I want to spawn this AItem later, as long as I have the value from MyItemBlueprint I can spawn it in the game like this:

UWorld* const World = ();
if (World){
    FActorSpawnParameters SpawnParams;
    SpawnParams.Instigator = this;
    AItem* DroppedItem = World->SpawnActor<ASurItem>(MyItemBlueprint, Location, Rotation, SpawnParams);
    if (DroppedItem){
        DroppedItem->DoTheThings();
    }
}

Hopefully that helps someone else!

3 Likes

I did not. I got it working the way above. I’m not sure if the spawning method of inventory is best, but I plan on having stacks of items in inventory up to 50 or more… Seems best not to Hide and maintain actors in the level.

Thanks @AnXgotta that worked well for me. Also a there a great example an snippet here A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

I’m glad it helped and thanks for pointing out that resource, I definitely missed it.

Hi AnXgotta, I am attempting to spawn blueprinted actors in code but cannot see them. I know that the actors are getting spawned, I just cannot see them at all. I am not sure if it is the same problem you were having with the static mesh component. Were you able to spawn your items with their static meshes? and if you were, do you have any tips on getting them to spawn with the static mesh that is attached to the blueprint?

Hey Jesse, I was able to spawn them with static meshes. I would check the spawn location of the object and make sure it is where you think it is. I would then ensure that the object isn’t hidden in any way.

What is ‘ASurItem’ that just comes out of the blue it seems… shouldn’t that be AItem ?

This is a bit old but if I remember correctly, it’s a child class of AItem

For those coming here from google, I was able to do this in a much simpler way:

// Step 1. Declare a property of type blueprint
UPROPERTY(EditAnywhere, Category = Players, meta = (AllowPrivateAccess = "true"))
TSubclassOf<class AActor> PlayerBlueprintType;

// Step 2. Wherever you need a UClass * reference for that blueprint (like SpawnActor)
AActor *player = ()->SpawnActor(PlayerBlueprintType.Get(), &playerStart->GetActorTransform(), params);

Just make sure you’re checking that PlayerBlueprintType (in this example) is non-null!

I do not understand the solution.
You want spawn the item from another actor (Player or Inventory). How this actor has the value of MyItemBlueprint?

When you extend this c++ class in blueprint, that variable will appear in the blueprint and you set it in there. IIRC, it will be exposed as a drop down selector.