How to spawn a Blueprint Actor in C++?
I know there are a bunch of questions like this, but can't find a way to solve this issue.
I have a AWeapon C++ class that I used to create a Blueprint (Child) for every weapon I wanted to implement in the game.
I'd like to spawn the Pistol Blueprint inside the BeginPlay() function inside of the PlayerCharacter class.
I heard about
But can't understand how/where to use this template function.
Some of them say that should be put into a Constructor.
Please, I need help.
asked Dec 14 '15 at 07:57 PM in C++ Programming
Yes, it needs to go inside the constructor.
You probably already have a reference to the pistol actor in your class. If not, you can declare it in the header file like this:
In the constructor, you need to tell FObjectFinder what type of class to expect, in this case UBlueprint. Then spawn an actor with the found object as template. You might want to add a check to make sure the object was actually found (in case you ever change the path):
Actually, for Blueprints the path might have to look like this: Blueprint'/Game/FirstPersonCPP/Blueprints/Pickup/Pistol.Pistol'
You can also use the ClassFinder directly, which works pretty similar to the ObjectFinder:
Okay, from what I gather is that you have a c++ PlayerCharacter class and that you setup your weapons as a c++ AWeapon class that you then create blueprints for.
Why not just create a member in the PlayerCharater class and expose it to blueprints. Then create a blueprint for PlayerCharacter, and then set the weapons there.
For example, in PlayerCharacter:
Then when you have BP_PlayerCharacter, you should see the property in the editor window.
If it is really something you need to spawn, then do something like:
This is basically what I do for weapon projectiles.
answered Dec 15 '15 at 04:04 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here