Why when playerController exists the user loses the game?

could someone help me? i’m supposed to have a winning and losing system and its a KillEmAllGameMode but when a pawn is killed i cast it to a PlayerController and if its not null ptr then the player loses?? I simply don’t understand why this happens.


void AKillThemAllGameMode::PawnKilled(APawn *PawnKilled)
{
    Super::PawnKilled(PawnKilled);
    APlayerController *PlayerController = Cast<APlayerController>(PawnKilled->GetController());

    if (PlayerController)
    {
        EndGame(false);
    }
}

void AKillThemAllGameMode::EndGame(bool bIsPlayerWinner)
{
    for (AController *Controller : TActorRange<AController>(GetWorld()))
    {
        bool bIsWinner = Controller->IsPlayerController() == bIsPlayerWinner;
        Controller->GameHasEnded(Controller->GetPawn(), bIsWinner);
    }
}

Maybe this will help? GameHasEnded Question - Unreal Courses / Ask - GameDev.tv

It doesn’t really help but I’m thinking that maybe when the controller is successfuly casted to a player controller then is that why it makes the player lose because it might be an AIController that you get?

Hmmmm… don’t have much experience with this code, but
I see that when you have Player Controller(Controller->IsPlayerController()) the bIsPlayerWinner will ALWAYS be false the way you called it and therefore bIsWinner will NEVER be true when you have the PlayerController.
Then in GameHasEnded(in APlayerController) since bIsWinner is ALWAYS false, you never win

What are you trying to do with PawnKilled? It seems as though if the Player is the pawn killed then you don’t end the game? because if PlayerController = true (which mean player is pawn killed), then EndGame = False?

the bIsWinner is false so the player loses

Basically is a kill em all game mode and im doing a course on udemy from gamedev.tv and thats what the code is so far but I don’t understand why the player loses if the player controller isn’t nullptr

I would think its because you call EndGame(false) only when you have the player controller. Then in your EndGame function you loop through all Actors in your GameWorld, in this loop you again test for PlayerController and when that is true, bIsPlayerWinner will be false, therefore bIsWinner is always false when PlayerController is true and bIsPlayerWinner is false. My thinking being that Controller->IsPlayerController() answers as TRUE when it is the player controller and false when it is not.

Well i’m going to make it so when all the ai is dead then the player wins but i only have lose right now

Ok well I’m pretty sure I understand from some help with other people, so if i’m correct the type is an APlayerController but if the PawnKilled is an AI then it will fail and return nullptr so it will only go through if its an AController rather than an AAIController

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms