Winning and Losing at the same time in Toon Tanks

Is there a way to prevent both the win and lose conditions being able to occur at the same time?


EDIT: I went and cloned the project to check out the code. Can you share your ToonTanksGameMode.cpp for me please.

Are you calling HandleGameover more than once? Thats the only thing I can think of right off the top of my head. Do you have a duplicate DisplayText block that was for blocking out your ui that has default text in it?

Add a print string function in your blueprint. Hook it up right before you create your widget. See how many times it prints if its more than once then use one of the methods listed below to find your problem.

You could put a breakpoint inside of the HandleGameover function and step through the code. Or if you don’t have the engine symbols installed you could just do a find (ctrl + F in visual studio). Search your “Current project” for all instances of HandleGameover. Don’t do entire solution or current document do Current project you might have accidently called it inside another class. Entire solution will take forever cause its going to go through the engine code also. If you find it being called more than once then that was your problem.

Hi Philip, thanks for your help. I will try that. Here is the file you asked for:

// Fill out your copyright notice in the Description page of Project Settings.

#include “ToonTanksGameMode.h”

#include “Kismet/GameplayStatics.h”

#include “Tank.h”

#include “Tower.h”

#include “ToonTanksPlayerController.h”

#include “TimerManager.h”

void AToonTanksGameMode::ActorDied(AActor *DeadActor)

{

if (DeadActor == Tank)

{

    Tank->HandleDestruction();

    if (ToonTanksPlayerController)

    {

        ToonTanksPlayerController->SetPlayerEnabledState(false);

    }

    GameOver(false);

}

else if (ATower* DestroyedTower = Cast<ATower>(DeadActor))

{

    DestroyedTower->HandleDestruction();

    --TargetTowers;

    if (TargetTowers == 0)

    {

        GameOver(true);

    }

}

FTimerDelegate TimerDel = FTimerDelegate::CreateUObject(this, &AToonTanksGameMode::BeginPlay);

}

void AToonTanksGameMode::BeginPlay()

{

Super::BeginPlay();

HandleGameStart();

}

void AToonTanksGameMode::HandleGameStart()

{

TargetTowers = GetTargetTowerCount();

Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));

ToonTanksPlayerController = Cast<AToonTanksPlayerController>(UGameplayStatics::GetPlayerController(this, 0));

StartGame();

if (ToonTanksPlayerController)

{

    ToonTanksPlayerController->SetPlayerEnabledState(false);

    FTimerHandle PlayerEnableTimerHandle;

    FTimerDelegate PlayerEnableTimerDelegate = FTimerDelegate::CreateUObject(

        ToonTanksPlayerController,

        &AToonTanksPlayerController::SetPlayerEnabledState,

        true

    );

    GetWorldTimerManager().SetTimer(PlayerEnableTimerHandle,

        PlayerEnableTimerDelegate,

        StartDelay,

        false

    );

}

}

int32 AToonTanksGameMode::GetTargetTowerCount()

{

TArray<AActor*> Towers;

UGameplayStatics::GetAllActorsOfClass(this, ATower::StaticClass(), Towers);

return Towers.Num();

}

everything lines up with the lecture’s code. Can you zip your project and share it with me. Either through google drive or another service. I’m curious as to what is causing your problem.

You could stick a DoOnce node between the event node and creating the widget.

1 Like

Privacy & Terms