Does the character of this code mean AI?

bool AShooterAIController::IsDead() const
{
    AShooterCharacter* ControlledCharacter = Cast<AShooterCharacter>(GetPawn());

    if (ControlledCharacter != nullptr)
    {
        return ControlledCharacter->IsDead();
    }

    return true;
}

I got Pawn from here. But does this Pawn mean AI here?

void AKillEmAllGameMode::EndGame(bool bIsPlayerWinner)
{

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

I also have a question from this code, if the player loses. bIsPlayerWinner becomes false, and since the player lost, you can’t use the controller separately.
Then will the Controller->IsPlayerController() be false? The player’s controller is disconnected, so I’ll check the remaining AI controllers. (It’s ambiguous to describe it in words.)

There, false, false is the same, so bIsWinner is true. Then you get into GameHasEnded like the player won.

I don’t understand this code. I don’t know if I don’t understand… Can you explain it easily?

What this code is doing is checking all Controllers (an special actor with functions for controlling pawn actors) to see which are or are not a player-owned controller. This check helps figure out which controller is really an AI controller or is specifically a Player Controller.

At that point, it uses bIsPlayerWinner to let the AI Controllers know if they lost or if they won because the player has been defeated. And then a function is called on each controller (both player and AI) that handles whether that controller won or lost.

Thank you for your response.

I have a question regarding the code’s functionality in determining the winner between the AI and the player. Specifically, does the code check the positions of both the AI and the player to decide the winner?

From the perspective of the player controller, if the player loses, the variable ‘bIsPlayerWinner’ is set to false, and ‘Controller->IsPlayerController()’ returns true since it is the player controller. In this scenario, does the variable ‘bIsWinner’ also become false, indicating a loss?

I couldn’t tell you exactly, but based on the name of the GameMode I suspect it’s based on either all of the AI being killed or the player being killed.

The key is to look at Controller->IsPlayerController() == bIsPlayerWinner . We can utilize something called a truth table to visualize what’s happening. But on observation alone, since this is a comparison boolean expression the result of bIsWinner would indeed be false, indicating a loss for the player.

When ‘bIsWinner’ is false, indicating a defeat, is it because ‘Controller->IsPlayerController()’ is true? Also, ‘bIsPlayerWinner’ is false.

Why is this so hard to understand…?

If the player loses then bWinner is false for the player and true for AI.

Player:
bIsWinner = true == false = false

AI:
bIsWinner = false == false = true

I think what makes it hard to understand is because the answer is contextual to either the player controller or an AI controller.

For the player to win, both IsPlayerController and bIsPlayerWinner need to be true, which makes bIsWinner equal to true.

For the AI to win, bIsWinner is true when IsPlayerController is false and bIsPlayWinner is false.

Any other combination means a loss:

Player Loss → bIsWinner is false when IsPlayerController is true but bIsPlayerWinner is false
AI Loss → bIsWinner is false when IsPlayerController is false but bIsPlayerWinner is true

Thank you for your response.

bIsPlayerWinner has the same value of player and AI. It doesn’t change the value you received as a parameter.
The only difference between the player and AI is the Controller->IsPlayer Controller() is the value of this part. It’s to make sure it’s a player controller.

Am I right to understand?

In other words, the AI case and the player case are calculated separately?

I’m sorry for not understanding

Right, it’s not modfified within the function so it’s whatever value it was when it was passed in here:

    if (PlayerController != nullptr)
    {
        EndGame(false); //Player loses, the rest of the code in this function isn't executed as the game ends before that can happen.
    }

    for (AShooterAIController* Controller : TActorRange<AShooterAIController>(GetWorld()))
    {
        if (!Controller->IsDead())
        {
            return;
        }
    }
    
    EndGame(true); // Player wins

Right, this is looping all controllers in the level, both AI and player. For player controllers that function would return true otherwise false.

Thank you for kindly explaining. Thanks to you, I understand now.

Privacy & Terms