I am trying to implement a teleport ability for my player character that has him teleport to the position of a projectile he shoots. I have it all coded and ready so that when the projectile is shot and the player right clicks on the mouse, they use the teleport function on the projectile and the projectile then casts to the player character, allowing it to change the player’s transform to be equal to that of he projectile. However, it seems that when I do this I end up with a nullptr and I inevitably crash.
This is in my player character header
void Ability();
UPROPERTY()
AProtoCharacter* playerActor;
UPROPERTY()
AProtoTProjectile* ProtoTBall;
I am calling this function through a BindAction in the player character cpp.
void AProtoCharacter::Ability()
{
ProtoTBall->BallTele(playerActor);
}
This is in my projectile header.
UFUNCTION()
void BallTele(class AActor* playerActor);
Finally, this is how I’m casting back to my player character from the projectile cpp. I only crash if I remove the if statement that checks if the playerActor is a nullptr.
void AProtoTBall::BallTele(class AActor* playerActor)
{
AProtoCharacter* player = Cast<AProtoCharacter>(playerActor);
UE_LOG(LogTemp, Warning, TEXT("Working"));
if (player != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("player pointer not nullptr"));
}
if (playerActor != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Has Teleported"));
player->SetActorRotation(GetActorRotation());
player->GetController()->SetControlRotation(player->GetActorRotation());
player->SetActorLocation(GetActorLocation());
}
}
I’ve tried having the key input be taken by the projectile instead but that doesn’t work.
The only other alternative I can think of is making it so that the Teleportation function on the projectile grabs its own transform, and when the player’s script calls over to it to just make the player characters transform equal the projectiles within the player characters script, instead of having the projectile change the player’s transform.
Any and all help would be greatly appreciated! I’m trying to get more experienced with C++ as I’m jumping to Unreal over from Unity. I have so much to learn, but I’m enjoying it!
Thanks in advance!