My pawn dont want to die

HI my pawn dont want die and I dont know why, help please.
HealthComponent.cpp

#include "HealthComponent.h"

#include "ToonTanks/GameModes/TanksGameModeBase.h"

#include "Kismet/GameplayStatics.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 = false;

    // ...

}

// Called when the game starts

void UHealthComponent::BeginPlay()

{

    Super::BeginPlay();

    Health = DefaultHealth; 

    GameModeRef = Cast<ATanksGameModeBase>(UGameplayStatics::GetGameMode(GetWorld()));

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

}

void UHealthComponent::TakeDamage(AActor* DamageActor, float Damage, const UDamageType* DamageType, AController* InstigatedBy, AActor* DamageCauser) 

{

    if (Damage==0 || Health <=0)

    {

        return;

    }

    Health = FMath::Clamp(Health - Damage, 0.0f, DefaultHealth);

    if (Health <= 0)

    {

        if (GameModeRef)

        {

            GameModeRef->ActorDied(GetOwner());

        }

        else

        {

            UE_LOG(LogTemp, Warning, TEXT("Health component has no reference to the GameMode"));

        }
     }
}

HealthComponent.h

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

#pragma once

#include "CoreMinimal.h"

#include "Components/ActorComponent.h"

#include "HealthComponent.generated.h"

class ATanksGameModeBase;

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )

class TOONTANKS_API UHealthComponent : public UActorComponent

{

    GENERATED_BODY()

private:

    UPROPERTY(EditAnywhere)

    float DefaultHealth = 100.0f;

    float Health = 0.0f;

    ATanksGameModeBase* GameModeRef;

public: 

    // Sets default values for this component's properties

    UHealthComponent();

protected:

    // Called when the game starts

    virtual void BeginPlay() override;

    UFUNCTION()

    void TakeDamage (AActor* DamageActor, float Damage, const UDamageType* DamageType, AController* InstigatedBy, AActor* DamageCauser);

};

TanksGameModeBase.cpp

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

#include "TanksGameModeBase.h"

#include "ToonTanks/Pawns/PawnTank.h"

#include "ToonTanks/Pawns/PawnTurret.h"

#include "Kismet/GameplayStatics.h"

#include "ToonTanks/PlayerControllers/PlayerControllerBase.h"

void ATanksGameModeBase::BeginPlay() 

{

    Super::BeginPlay();

    //Get reference and game win/lose conditions.

    //Call HandleGameStart() to initialise the start countdown, turret activation, pawn check etc. 

    HandleGameStart();   

}

void ATanksGameModeBase::ActorDied(AActor* DeadActor) 

{

    // if (DeadActor == PlayerTank)

    // {

    //     PlayerTank->HandleDestruction();

    //     HandleGameOver(false);

    //     if (PlayerControllerRef)

    //     {

    //         PlayerControllerRef->SetPlayerEnabledState(false);

    //     }        

    // }

    // else if (APawnTurret* DestroyedTurret = Cast<APawnTurret>(DeadActor))   

    // {

    //     DestroyedTurret->HandleDestruction();

    //     if (--TargetTurrets == 0 )

    //     {

    //         HandleGameOver(true);

    //     }        

    // }    

    UE_LOG(LogTemp, Warning, TEXT("Died!!!!"));

}

void ATanksGameModeBase::HandleGameStart() 

{

    TargetTurrets = GetTargetTurretCount();

    PlayerTank = Cast<APawnTank>(UGameplayStatics::GetPlayerPawn(this, 0));

    PlayerControllerRef = Cast<APlayerControllerBase>(UGameplayStatics::GetPlayerController(this, 0));

    //Initialise then start countdown, turret cativation, pawn check etc.

    //Call Plueprints version GameStart();

    GameStart();

       if (PlayerControllerRef)

        {

            PlayerControllerRef->SetPlayerEnabledState(false);

            FTimerHandle PlayerEnableHandle;

            FTimerDelegate PlayerEnableDelegate = FTimerDelegate::CreateUObject (PlayerControllerRef, 

                &APlayerControllerBase::SetPlayerEnabledState, true);

            GetWorld()->GetTimerManager().SetTimer(PlayerEnableHandle, PlayerEnableDelegate, StartDelay, false);

        }    

}

void ATanksGameModeBase::HandleGameOver(bool PlayerWon) 

{

    //See if the Player has destroyed all the turrets, show win results.

    //else if turret destroyed player, show lose result.

    //Call blueprint version GameOver(boll).

    GameOver(PlayerWon);

}

int32 ATanksGameModeBase::GetTargetTurretCount() 

{

    TArray<AActor*> TurretActors;

    UGameplayStatics::GetAllActorsOfClass(GetWorld(), APawnTurret::StaticClass(), TurretActors);

    return TurretActors.Num();

}

ProjectileBase.h

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

#pragma once

#include "CoreMinimal.h"

#include "GameFramework/Actor.h"

#include "ProjectileBase.generated.h"

class UProjectileMovementComponent;

UCLASS()

class TOONTANKS_API AProjectileBase : public AActor

{

    GENERATED_BODY()

private:

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "True"))    

    UProjectileMovementComponent* ProjectileMovement;

    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "True"))

    UStaticMeshComponent* ProjectileMesh;

    UPROPERTY(EditDefaultsOnly, Category = "Damage")

    TSubclassOf<UDamageType> DamageType;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Move", meta = (AllowPrivateAccess = "True"))

    float MovementSpeed = 1300.0f;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Damage", meta = (AllowPrivateAccess = "True"))

    float Damage = 50.0f;

    UFUNCTION()

    void OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);

    

public: 

    // Sets default values for this actor's properties

    AProjectileBase();

protected:

    // Called when the game starts or when spawned

    virtual void BeginPlay() override;

};

ProjectileBase.cpp

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

#include "ProjectileBase.h"

#include "Components/StaticMeshComponent.h"

#include "GameFramework/ProjectileMovementComponent.h"

#include "Kismet/GameplayStatics.h"

#include "Particles/ParticleSystemComponent.h"

// Sets default values

AProjectileBase::AProjectileBase()

{

    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.

    PrimaryActorTick.bCanEverTick = false;

    ProjectileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Projectile Mesh"));

    ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectileBase::OnHit);

    RootComponent = ProjectileMesh;

    ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement"));

    ProjectileMovement->InitialSpeed = MovementSpeed;

    ProjectileMovement->MaxSpeed = MovementSpeed;

    InitialLifeSpan = 1.0f;

}

// Called when the game starts or when spawned

void AProjectileBase::BeginPlay()

{

    Super::BeginPlay();

    

}

void AProjectileBase::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) 

{

    //Try to get a referance to the owning class.

    AActor* MyOwner = GetOwner();

    //If for some reason we can`t get a valid reference, return as we need to check against the owner.

    if (!MyOwner)

    {

        return;

    }

    //If thw actor ISN`T self OR Owner AND exists, then apply damage.

    if (OtherActor && OtherActor != this && OtherActor != MyOwner)

    {

        UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwner->GetInstigatorController(), this, DamageType);

    }

    

    //Play a bunch of effects here during the polish phase. - TODO

    Destroy();  

}

I try to solve problem and going to next lessons but can`t make it.

It is like I have some invisible wall and cant hit the pawn

By pawn you mean both? If you add these two logs do they both output?

void AProjectileBase::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) 
{
    UE_LOG(LogTemp, Warning, TEXT("OnHit called"))
    //Try to get a referance to the owning class.
    AActor* MyOwner = GetOwner();
    //If for some reason we can`t get a valid reference, return as we need to check against the owner.
    if (!MyOwner)
    {
        return;
    }

    //If thw actor ISN`T self OR Owner AND exists, then apply damage.
    if (OtherActor && OtherActor != this && OtherActor != MyOwner)
    {
        UE_LOG(LogTemp, Warning, TEXT("%s hit %s"), *MyOwner->GetName(), *OtherActor->GetName())
        UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwner->GetInstigatorController(), this, DamageType);
    }
    
    //Play a bunch of effects here during the polish phase. - TODO
    Destroy();  
}

Also in the future please use a code block by using the </> button or surrounding it in three backticks.

Yep both pawns dont show any logs and dont take hit…


Did you create the projectile blueprint before adding this line?

It was alrady in project

I made all like in video lessons, and went further becouse thinking solve in the next lessons

If you delete the projectile blueprint and re-create it do you get the logs?

when re-create I dont have any logs and now bullet dont spawn)

uuups sory all work normally last my falt forget add projectile base to another bp

Why it is solve when I re-create?

Because of the same reason why when you change a value in the C++ constructor it doesn’t get propagated to already made blueprints that are derived from that class; it just sets the new default which you can reset to.

oh thanks

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

Privacy & Terms