ToonTank Section

Hi all,
After finishing the EndGameWidget lecture, my game is acting weird - the camera position is changed, gameplay functionalities are gone (can’t move, shoot, etc.). Game practically gets stuck as soon as starting message is shown. My TankGameMode.h and .cpp code is as below. I would appreciate any pointers since I cannot seem to understand what’s causing the issue.

Header file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "TankGameModeBase.generated.h"

class APawnTurret;
class APawnTank;
UCLASS()
class TOONTANKS_API ATankGameModeBase : public AGameModeBase
{
	GENERATED_BODY()


private:
	APawnTank* PlayerTank;
	int32 TargetTurrets = 0;

	int32 GetTargetTurretCount();
	void HandleGameStart();
	void HandleGameOver(bool PlayerWon);


public:

	void ActorDied(AActor* DeadActor);

protected:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Game Loop")
	int32 StartDelay = 3;

	virtual void BeginPlay() override;
	UFUNCTION(BlueprintImplementableEvent)
	void GameStart();
	UFUNCTION(BlueprintImplementableEvent)	
	void GameOver(bool PlayerWon);
};

Implementation:

#include "TankGameModeBase.h"
#include "ToonTanks/Pawns/PawnTank.h"
#include "ToonTanks/Pawns/PawnTurret.h"
#include "Kismet/GameplayStatics.h"

void ATankGameModeBase::BeginPlay() 
{
    Super::BeginPlay();
    
    HandleGameStart();

}

void ATankGameModeBase::ActorDied(AActor* DeadActor) 
{
    if(DeadActor == PlayerTank)
    {
        PlayerTank->HandleDestruction();
        HandleGameOver(false);
    }
    else if(APawnTurret* DestroyedTurret = Cast<APawnTurret>(DeadActor))
    {
        DestroyedTurret->HandleDestruction();

        if(--TargetTurrets == 0)
        {
            HandleGameOver(true);
        }
    }
}

void ATankGameModeBase::HandleGameStart() 
{
    TargetTurrets = GetTargetTurretCount(); 
    PlayerTank = Cast<APawnTank>(UGameplayStatics::GetPlayerPawn(this, 0));

    GameStart();
}

void ATankGameModeBase::HandleGameOver(bool PlayerWon) 
{
    GameOver(PlayerWon);
}


int32 ATankGameModeBase::GetTargetTurretCount() 
{
    TArray<AActor *> TurretActors;
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), APawnTurret::StaticClass(), TurretActors);
    return TurretActors.Num();
}

I think that it would be easier if I could just take a look at your project.

Could you send me your project by using the following link?

Please use File > Package Project > Zip Up Project for creating the zip as it will only include necessary files.

https://gdev.tv/projectupload

Many many thanks Dan.

I uploaded the zip file as requested. I know that I’m most probably missing something minor, and will feel very dumb when you point it out; but I couldn’t find it on my own. :sweat_smile:
Thanks again.

So what I have was

  • HealthComponent was derived from AActor instead of UActorComponent.
    Changing this
     class TOONTANKS_API AHealthComponent : public AActor
    
    to
    class TOONTANKS_API UHealthComponent : public UActorComponent
    
    Note the change from A to U. And then update all uses of AHealthComponent to UHealthComponent.
  • In the countdown function this is missing the function name (Countdown) and the StartDelay for the Time

From there I’m a bit lost. The tank isn’t spawning with what you’ve given me which is perplexing. Is that not the case for you? It doesn’t sound like it from your initial post.

Many thanks Dan for your reply.

Actually the AHealthComponent was mistakenly created, and therefore I created a HealthComponent_f afterward which is a UHealthComponent as you mentioned. But the weird thing is, that the AHealthComponent should not show up anywhere as I used the fixed one. Could you please let me know where you found it being used so I can replace it again? I searched the code files and the only places that it’s used is in its own header and implementation, and nowhere else in the files that are actually being used.

Oh that’s silly of me missing the function name. Thanks for pointing it out, I fixed it.

About the TankPawn not spawning, that is actually what I meant by losing functionality; I misstated the situation. Any thoughts on what’s causing this? :yum:

Oh I just got a warning when compiling is all.

So I figured it out, it’s not that you’re not spawning a tank. You’re doing something peculiar in your tank’s BeginPlay. Do you notice anything strange about that?

LOL, why don’t we destroy the player as soon as they’re spawned? (Next DOOM idea? xD)

All fixed now, many many thanks for the help, Dan!
You’re a legend.