Can I Spawn a BP from CPP?

Hi! So I’ve been trying to spawn in another CPP actor using my CPP actor manager. It works perfectly but the thing is that I do not specify many aspects of the actor at runtime in the CPP because I like to configure the settings in blueprint before using it. I would like to spawn in the blueprint instance of this actor.

How would I do that in CPP? Thanks a ton.

Ofcorse, first you need to realize that blueprint is a class same as C++ class… so yes that means every blueprint as any other class has it’s own UClass which you can do SpawnActor, it’s also importent to understand that same as you extend class C++, blueprint also extends class including C++ once. Go see “Class Viewer” in Windows->Devlopment Tools->Class Viewer and search your blueprint there and you will be able to see class relation tree of your blueprint, it should open your eyes. Only limitation is you can’t use blueprint-made object directly as type in C++, as compiler don’t know how to interact with virtual classes and objects like blueprints. you need to use most common C++ class related to blueprint insted.

There 2 ways to get UClass of blueprint:

  1. Most easiest one is getting UClass by setting it in property in property editor, like blueprints defaults for example. So depending on your designm in C++ make UPROPERTY(EditAnywhere) of type UClass* or TSubclassOf<ASomeClass> somewhere that code that gonna spawn can access, like your AGameMode for example and then make a blueprint and set property you made, now call SpawnActor using that property as class input. You can extend AWorldSettings to add property ot World Settings of a level, you can then get WorldSettings variables like a described here: How do you access "World Settings" variables in blueprint event graph? - Blueprint - Epic Developer Community Forums
  2. 2nd option is to FClassFinder which you can see example of use here: C++ Reference to Widget Component without Editor - UI - Epic Developer Community Forums Limitation here is that you can do that only in class constructor

1 option is most safest since referencing assets direly in C++ may cause error in packaging, aspecially if C++ code is only thing referencing to asset, then it is not packaged and cause error in packaged game.