Errors In Toon Tanks

Look:



















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"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TOONTANKS_API UHealthComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UHealthComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

private:
	UPROPERTY(EditAnywhere)
	float MaxHealth = 100.f;
	float Health = 0.f;

	UFUNCTION()
	void DamageTaken(AActor* DamagedActor, float Damage, UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser);

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

		
};

HealthComponent.cpp:

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


#include "HealthComponent.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);
}


// Called every frame
void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, UDamageType* DamageType, AController* Instigator, AActor* DamageCauser);
{
	if (Damage <= 0.f) return;

	Health -= Damage;

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

Projectile.h:

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Projectile.generated.h"

UCLASS()
class TOONTANKS_API AProjectile : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AProjectile();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	UPROPERTY(EditDefaultsOnly, Category = "Combat")
	UStaticMeshComponent* ProjectileMesh;

	UPROPERTY(VisibleAnywhere, Category = "Movement")
	class UProjectileMovementComponent* ProjectileComp;

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

	UPROPERTY(EditAnywhere)
	float Damage = 50.f;
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	
};

Projectile.cpp:

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

#include "Projectile.h"
#include "GameFramework/ProjectileMovementComponent.h
#include "GameFramework/DamageType.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
AProjectile::AProjectile()
{
 	// 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"));
	RootComponent = ProjectileMesh;

	ProjectileComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("Projectile Movement Component"));
	ProjectileComp->InitialSpeed = 1000.f;
	ProjectileComp->MaxSpeed = 1000.f; 
}

// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
	Super::BeginPlay();
	
	ProjectileMesh->OnComponentHit.AddDynamic(this, &AProjectile::OnHit);
}

// Called every frame
void AProjectile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void AProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector OtherImpulse, const FHitResult& Hit)
{
	auto MyOwner = GetOwner();
	if (MyOwner == nullptr) return;

	AController* MyOwnerInstigator = MyOwner->GetInstigatorController();
	UClass* DamageTypeClass = UDamageType::StaticClass();

	if (OtherActor && OtherActor != this && OtherActor != MyOwner)
	{
		UGameplayStatics::ApplyDamage(OtherActor, Damage, MyOwnerInstigator, this, DamageTypeClass);
		Destroy();
	}
}

How do I fix the errors?

You have a semicolon on the end of the first line here. Remove it.

P.S. Please don’t post error message over a series of screenshots (18 here!) in the future. Copy and paste it.

It still has an error:
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(24) : error C2064: term does not evaluate to a function taking 0 arguments
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(38) : error C2562: ‘UHealthComponent::DamageTaken’: ‘void’ function returning a value

Hello? How do I fix the error?

This says on line 24 in HealthComponent.cpp you tried to call a function with 0 arguments but there is no such function that takes 0 arguments.

Example:

int square(int n)
{
    return n * n;
}

int main()
{
    int value = square(); // ERRROR square doesn't take 0 arguments
    return 0;
}

This says on line 38 in HealthComponent.cpp you tried to return a value from a void function, void functions do not return a value.

Example:

void foo()
{
    return; // OK, only returns, not with a value.
    return 1; // ERROR void function can't return any value
}

Please learn to be patient and allow 48 hours before bumping your thread.

Note: This doesn’t mean it will take 48 hours for me to reply.

Look:
Building ToonTanksEditor…
Using Visual Studio 2022 14.30.30705 toolchain (C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).
Building 5 actions with 8 processes…
[1/5] HealthComponent.cpp
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(26) : error C2064: term does not evaluate to a function taking 0 arguments
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(28) : error C2440: ‘’: cannot convert from ‘AGameModeBase *’ to ‘AToonTanksGameMode’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(28): note: No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(28) : error C2059: syntax error: ‘;’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(29) : error C2143: syntax error: missing ‘;’ before ‘}’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(33) : error C2146: syntax error: missing ‘>’ before identifier ‘ThisTickFunction’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(34) : error C2143: syntax error: missing ‘;’ before ‘{’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(35) : error C2065: ‘TickType’: undeclared identifier
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(35) : error C2065: ‘ThisTickFunction’: undeclared identifier
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(35) : error C2059: syntax error: ‘)’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(38) : error C2143: syntax error: missing ‘;’ before ‘}’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(40) : error C2146: syntax error: missing ‘>’ before identifier ‘DamageType’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(40) : error C2146: syntax error: missing ‘>’ before identifier ‘Instigator’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(40) : error C2146: syntax error: missing ‘>’ before identifier ‘DamageCauser’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(41) : error C2143: syntax error: missing ‘;’ before ‘{’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(42) : error C2947: expecting ‘>’ to terminate template-argument-list, found ‘<=’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(42) : error C2062: type ‘unknown-type’ unexpected
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(42) : error C2059: syntax error: ‘)’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(48) : error C2065: ‘DamagedActor’: undeclared identifier
[2/5] ToonTanksGameMode.cpp
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\ToonTanksGameMode.cpp(11) : error C2065: ‘DeadActor’: undeclared identifier
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\ToonTanksGameMode.cpp(18) : error C2039: ‘GetPlayerController’: is not a member of ‘ATank’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\Tank.h(13): note: see declaration of ‘ATank’
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\ToonTanksGameMode.cpp(22) : error C2065: ‘DeadActor’: undeclared identifier
Look:
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"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TOONTANKS_API UHealthComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UHealthComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

private:
	UPROPERTY(EditAnywhere)
	float MaxHealth = 100.f;
	float Health = 0.f;

	UFUNCTION()
	void DamageTaken(AActor* DamagedActor, float Damage, UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser);

	class AToonTanksGameMode* ToonTanksGameMode;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

};

HealthComponent.cpp:

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


#include "HealthComponent.h"
#include "Kismet/GameplayStatics.h"
#include "ToonTanksGameMode.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, UDamageType* DamageType, AController* Instigator, AActor* DamageCauser)
{
	if (Damage <= 0.f) return

	Health -= Damage;

	if (Health <= 0.f && ToonTanksGameMode)
	{
		ToonTanksGameMode->ActorDied(DamagedActor);
	}
}

ToonTanksGameMode.h:

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

#pragma once

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

/**
 * 
 */
UCLASS()
class TOONTANKS_API AToonTanksGameMode : public AGameModeBase
{
	GENERATED_BODY()

public:
	void ActorDied(AActor* DeadActor);

protected:
	virtual void BeginPlay() override;
	
private:
	class ATank* Tank;
};

ToonTanksGameMode.cpp:

// 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"

void AToonTanksGameMode::ActorDied(AActor* ActorDied)
{
    if (DeadActor == Tank)
    {
        Tank->HandleDestruction();

        if (Tank->GetTankPlayerController())
        {
            Tank->DisableInput(Tank->GetTankPlayerController());
            Tank->GetPlayerController()->bShowMouseCursor = false;
        }
    }

    else if (ATower* DestroyedTower = Cast<ATower>(DeadActor))
    {
        DestroyedTower->HandleDestruction();
    }
}

void AToonTanksGameMode::BeginPlay()
{
    Super::BeginPlay();

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

Please help!

HealthComponent.cpp:

Line 26 OnTakeAnyDamage isn’t a function, remove the ()
Line 28 The closing > is missing on the Cast
Line 42 missing ; after return

ToonTanksGameMode.cpp:

Line 11 DeadActor is undefined, you called your parameter ActorDied. I suggest you rename it to DeadActor.

There is one more error!
C:\Unreal Engine C++ Developer Learn C++ and Make Video Games\ToonTanks\Source\ToonTanks\HealthComponent.cpp(26) : error C2664: ‘void TSparseDynamicDelegate<FTakeAnyDamageSignature_MCSignature,AActor,FTakeAnyDamageSignatureInfoGetter>::__Internal_AddDynamic(UserClass ,void (__cdecl UHealthComponent:: )(AActor *,float,const UDamageType *,AController *,AActor ),FName)’: cannot convert argument 2 from 'void (__cdecl UHealthComponent:: )(AActor *,float,UDamageType *,AController *,AActor )’ to 'void (__cdecl UHealthComponent:: )(AActor *,float,const UDamageType *,AController *,AActor *)’
[

That looks like you missed const on UDamageType*

void DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser);
1 Like

Thank you! Now I have no errors!

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

Privacy & Terms