Search code examples
componentsunreal-engine4

Unreal engine UPROPERTY TsubclassOf not recognized


When I've created an ActorComponent c++ subclass with TSubclassOf UPROPERTY and set this class in blueprint I can't read read this property in c++ constructor. In .h file I've got this:

protected:
    UPROPERTY(EditAnywhere, Category = "Setup")
        TSubclassOf<UBaseSkill> PrimarySkillClass;

And this in .cpp:

USkillSet::USkillSet()
{
if(PrimarySkillClass.Get())
    {
        UE_LOG(LogTemp, Warning, TEXT("Creating skill"));
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("No skill class"));
    }
}

In BP I'm setting a class: BP screenshot so PrimarySkillClass.Get() should return true, but I'm getting "No skill class" in log. Why and how could I fix this?


Solution

  • The constructor is the first method ever called on an object, it's to early on the life of the UObject. Properties, values from Blueprints and components are initialized afterwards.

    So you have to access them later, either on UObject::PostInitProperties() or on BeginPlay() if it's not too late for your purpose.

    Edit: interesting reading on UObject Constructor, PostInitProperties and PostLoad