Download

Property initialized from Cast becomes null ?

When I initialize an ActorComponent property from a cast, and try to access it in a TickComponent I get nullptr :



void UMovementComp::InitializeComponent()
{
    Super::InitializeComponent();

    AActor *owner = GetOwner();

    if (owner == nullptr) {
        PrimaryComponentTick.bCanEverTick = false;
        return;
    }
    this->character = Cast<ABaseCharacter>(owner);

    if (this->character == nullptr) {

        PrimaryComponentTick.bCanEverTick = false;
        UE_LOG(LogTemp, Warning, TEXT("Cast to our character instance failed, component tick disabled, path = %s"), *GetPathName());
    }
}

void UMovementComp::CameraRotation()
{
    UE_LOG(LogTemp, Warning, TEXT("CameraRotation"));

    if (this->character == nullptr) {
        UE_LOG(LogTemp, Warning, TEXT("character is null"));  // always triggers
        return;
    }
    else if (character->controls == nullptr) {
        UE_LOG(LogTemp, Warning, TEXT("controls are null"));
        return;
    }

    FControls *controls = character->controls;
    UInputComponent *input = character->InputComponent;

    FRotator delta_rotation(0);
    delta_rotation.Yaw = input->GetAxisKeyValue(controls->mouse_x_key) * controls->mouse_x_sensitivity;
    delta_rotation.Pitch = input->GetAxisKeyValue(controls->mouse_y_key) * controls->mouse_y_sensitivity;

    character->AddActorLocalRotation(delta_rotation);
}


Apparently InitializeComponent is unable to change class properties regardless of whether they are marked with UPROPERTY() or not and I have to use BeginPlay