ugameplaystatics::getplayercharacter(GetWorld(), 0)->GetName() crashes editor

I am attempting to match an instance my pawn at player0 when an object this function is in detects it has been collided into by the pawn at player0. I tried to use ugameplaystatics::getplayercharacter(GetWorld(), 0); at first. I can use this and do not have any problems with the editor crashing as soon as it hits this line. The only problem however is that it doesn’t seem to be able to work in this expession;

void AAcceleratorPrayerOrb::OnOverlap
UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComponent,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult
// It doesn’t work in this if statement. It never evaluates to true.
if ( OtherActor == ugameplaystatics::getplayercharacter(GetWorld(), 0) );

If I try to program around this however and use:

ugameplaystatics::getplayercharacter(GetWorld(), 0)->GetName();

or any other method in GetPlayerCharacter, it crashes instantly as soon as it hits this line.

I just found out that ugameplaystatics::getplayercharacter(GetWorld(), 0) is a null pointer. I’m not sure why though. I can get a non null pointer if I do:

FString PawnName;

for (TActorIterator<APawn> ActorItr(GetWorld()); ActorItr; ++ActorItr)
    APawn *pawn = *ActorItr;
    PawnName = ActorItr->GetName();

I was thinking that too but I got it to work once. I’m following a tutorial that does the same thing. The only difference is that they are using the first person player and/or the FPS template while I’m using the flying template. Maybe the flying template isn’t a character? Maybe I should try get pawn and see how that works?

I think the flying template is not character. Getpawn should work tho.

I think you’re right. That’s probably why getting a pawn while iterating through APawn works and trying to use GetName() on a playercharacter fails and is a null pointer.

PlayerCharacter is different from PlayerPawn and different from PlayerController. Character is a thing that has 2 legs (just google differences, i am bad at explaining that xd). Your level probably has no character but a pawn is standard. I think default you will control a camera which acts as pawn. But the camera is not a character

I know that ACharacter is a subclass of AActor. I also think that APawn is a subclass of AActor. I’m not sure how I can either of them as an AActor if that would work. If I want to compare OtherActor an AActor with GetPlayerCharacter which is an ACharacter, I think I would have to cast ACharacter to an AActor if that’s possible.

I wouldn’t rely on UGameplayStatics::GetPlayerCharacter(0).

If you want to make sure it is the exact character, pass a reference of that Character into the Actor and then check against OtherActor.

Or, if you simply want to see if Other actor is a Character, you could do:

ACharacter *OtherCharacter = Cast<ACharacter>(OtherActor);
    // Your code here...

Thanks for the comment. I think I get it now. I think I was getting a null pointer because I was trying to use GetPlayercharacter on the flying pawn C++ template that doesn’t have an ACharacter. When I switched to GetPlayerPawn, everything worked as expected.

Thanks. I will try this. BTW, I was using ugameplaystatics::getplayerpawn(GetWorld(), 0). I’m using the flying template and there was no player character. When I tried to use player character with the default flying template, I got a null reference back. I’m also having issue trying to run my build directly from my IDE. I created a question for it. I used to be able to run my game directly from my IDE before. I need to be able to run it from my IDE so I can figure out why my game is crashing.