Hi all,
I have finally decided to ask my first question. I am trying to do something that I thought was intuitive but turns out, it is not.
I am trying to instantiate my blueprint derived Actor Component from C++. This works very easily for normal Actors, as I can just call SpawnActor and hit the ground running.
To lay out a simple example:
(in .h)
UPROPERTY(EditDefaultsAnywhere, BlueprintVisibleAnywhere)
TSubclassOf healthCompClass_;
UHealthComponent* healthComp_;
(in .cpp)
healthComp_ = /* THE CODE IN QUESTION */
healthComp->RegisterComponent();
The code above is the code I am messing with in my Player class. Now, I am under the impression that derived blueprints are not available at compile time, only at run time, so CreateDefaultSuboject is not a valid solution. With that being said, after crashing the editor 5109 times, I have come to the forums.
It seems like things like this MUST be done in BeginPlay in order to have a reference to the blueprint derived class. I have come to that realization based upon not being able to use CreateDefaultSubobject in the constructor. I have done this successfully with Actors I am spawning at runtime but these Actor Components need to be created when the gameplay starts (and apparently calling SpawnActor is also not valid when working with Actor Components).
Can anyone give some insight on best practices and/or how they have got this to work on the past? It is driving me nuts because it doesn’t seem like it should be that difficult!?