How can I initialization My BP_Bullet in C++?

Hello, I have a question when initializing in Unreal.
I created a bullet C++ class for BP_Player to fire.

And I followed the example of creating a variable called bulletFactory and assigning Bullet in Details of BP_Player in Unreal Editor.

UPROPERTY(EditDefaultsOnly,Category=BulletFactory)
TSubclassOf<class ABullet> bulletFactory;

image

What I want to know here is, is it not possible to assign BP_Bullet in C++ code rather than assigning BP_Bullet in Unreal Editor?

Like the code below

AMyPlayer::AMyPlayer()
{	
	bulletFactory = 

}

It is possible, yes, but keep in mind that the Blueprint implementations don’t exist at the time that C++ is compiled, so you have to get a little creative. Assuming that your BP_Bullet was at /Game/Factories/BP_Bullet in your overall game content, it would be something like:

AMyPlayer::AMyPlayer()
{	
    static ConstructionHelpers::FClassFinder<ABullet> BulletFactoryBPClass(TEXT("/Game/Factories/BP_Bullet"));
    if (BulletFactoryBPClass.Class != nullptr)
    {
        BulletFactory = BulletFactoryBPClass.Class;
    }
}

Basically, you need to go find the blueprint on-the-fly at runtime, and then assign the class that way.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.