Hi, everyone!
I"m trying to interact with blueprint Actor. For that I created UActorComponent, and called this InteractComponent. In InteractComponent I made variable bInteract. For blueprint Actor I added this component, and set bInteract to true.
For interaction I changed NewPlayerController.ccp
void ANewPlayerController::MoveToMouseCursor()
{
// Trace to see what is under the mouse cursor
FHitResult Hit;
UInteractComponent* Interact = Cast<UInteractComponent>(Hit.GetActor());
if (Interact)
{
if (Interact->bInteract == true)
{
UE_LOG(LogTemp, Warning, TEXT("CAN INTERACT"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("CAN'T INTERACT"));
}
}
if (Interact == NULL)
{
GetHitResultUnderCursor(ECC_Visibility, false, Hit);
if (Hit.bBlockingHit)
{
// We hit something, move there
SetNewMoveDestination(Hit.ImpactPoint);
UE_LOG(LogTemp, Warning, TEXT("NO INTERACT!"));
}
}
}
But my cast failed every time, I don’t understand why.
If there is a hit, then this hit collided with specific component of actor.
FHitResult actually contains info about component it hit. It can be accessed in two ways:
hit.GetComponent()
hit.Component.Get()
void AMyProjectPlayerController::MoveToMouseCursor()
{
// Trace to see what is under the mouse cursor
FHitResult Hit;
UInteractComponent* Interact = Cast<UInteractComponent>(Hit.GetActor()->GetComponentByClass(UInteractComponent::StaticClass()));
if (Interact)
{
UE_LOG(LogTemp, Warning, TEXT("Cust sucess"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Cust faild"));
}
}
And this line - UInteractComponent* Interact = Cast(Hit.GetActor()->GetComponentByClass(UInteractComponent::StaticClass())) crashed plugin.
Secondly Nachtmahr mentioned a right thing, where is Hit being initialized? Where and how you retrieve info to this Hit struct? If you just create FHitResult in variable, then it will be completely empty, Cast will always fail.
Thank you, guys! Yep, I made really big mistake when I created empty FHitResult and tried to cast to component. =.="
So in my case this is working code:
void ANewPlayerController::MoveToMouseCursor()
{
FHitResult Hit(ForceInit);
GetHitResultUnderCursor(ECollisionChannel::ECC_WorldDynamic, false, Hit);
UInteractComponent* Interact = Cast<UInteractComponent>(Hit.GetActor()->GetComponentByClass(UInteractComponent::StaticClass()));
if (Interact)
{
if (Interact->bInteract == true)
{
UE_LOG(LogTemp, Warning, TEXT("CAN INTERACT"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("CAN'T INTERACT"));
}
}
else
{
if (Hit.bBlockingHit)
{
// We hit something, move there
SetNewMoveDestination(Hit.ImpactPoint);
UE_LOG(LogTemp, Warning, TEXT("NO INTERACT!"));
}
}
}
I can’t use Hit.GetComponent(), because when I click to Actor I click to Mesh component (or maybe any collision object), but my component no Mesh it just code (I forgot to mention).