Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Toon Tanks

Destroy the turrets, avoid their projectiles. · By OddPhishGames

Adding Health Bars

A topic by djderrickg created Feb 27, 2024 Views: 57 Replies: 3
Viewing posts 1 to 4
(+1)

Hi there,

I love what you've done here and I'm also trying to expand my version of Toon Tanks. I'm having a heck of a time figuring out how to get my health bars to work. Any tips on your implementation? 

Thanks!

Developer

Hi Derrick, thanks for checking out the game :) It's been a while since I created this but I am trying to open my UE project so I can post some images in this thread.  

For some reason the project file is not compiling, it says I need to rebuild because it was created in a different version. 

As soon as I get it open I will post some images. 

Developer

Here is my health code, a C++ Class. Tbh I can't explain it very well because its been a while since I did any programming. 

What might help you is to copy this code and paste it into Chat GPT, it should do a good job of explaining it. I will post the code in text in my next comment instead of an image.


Developer

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

#include "HealthComponent.h"

#include "Kismet/GameplayStatics.h"

#include "ToonTanksGameMode.h"

#include "Tank.h"

// Sets default values for this component's properties

UHealthComponent::UHealthComponent()

{

// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features

// off to improve performance if you don't need them.

PrimaryComponentTick.bCanEverTick = true;

// ...

}

// Called when the game starts

void UHealthComponent::BeginPlay()

{

Super::BeginPlay();

Health = MaxHealth;

GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::DamageTaken);

ToonTanksGameMode = Cast<AToonTanksGameMode>(UGameplayStatics::GetGameMode(this));

}

// Called every frame

void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)

{

Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

// ...

}

void UHealthComponent::DamageTaken(

AActor* DamagedActor, 

float Damage, 

const UDamageType* DamageType, 

class AController* Instigator, 

AActor* DamageCauser)

{

if (Damage <= 0.f) return;

Health -= Damage;

UE_LOG (LogTemp, Error, TEXT("Health: %f"), Health);

if (Health <= 0.f && ToonTanksGameMode) 

{

UE_LOG (LogTemp, Warning, TEXT("Dead"));

ToonTanksGameMode->ActorDied(DamagedActor);

}

}