Hi, I’m learning to write code in C++ and also learning UnrealEngine 5 and its api for C++, now I’m practicing on a personal project, trying to make a 3-person shooter, just for learning.Today I came across a problem that is beyond my understanding(. I don’t quite understand how to ask a question here and whether it is possible to attach files. So for now I will try to describe how I tried to explain this problem to AI)
Further everything I described to AI, it is in complete agreement with my order of trying to solve this problem:
"1>------ Building 6 action(s) started ------
1>[1/6] Compile [x64] TutorialRifleWeapon.cpp
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C3646: ‘CharacterDead’: unknown override specifier
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>[2/6] Compile [x64] AIRifleWeapon.cpp
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C3646: ‘CharacterDead’: unknown override specifier
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>[3/6] Compile [x64] TutorialBaseWeapon.cpp
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C3646: ‘CharacterDead’: unknown override specifier
1>F:\aue\ShooterTutorial\Source\ShooterTutorial\Public\Weapon\TutorialBaseWeapon.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>Total time in Parallel executor: 2.30 seconds
I haven’t changed or touched the CharacterDead delegate in any way, and suddenly it started generating an error. Don’t reply to this message. Now I will send you the code of two files TutorialBaseWeapon.h and TutorialBaseWeapon.cpp.".
The AI started to advise me various options, but nothing fit, either it advised me nonsense or I already had this code written. At that time I didn’t realize which code was causing this error.
“Okay” I thought, I have a saved version before these changes I made, let’s try the changes in order and see where the culprit is.
Next, I’m dropping the code for the files involved:
TutorialBaseCharacter.h header where the delegate is declared:
// Game. All Rights Reserved.
#pragma once
include “CoreMinimal.h”
include “GameFramework/Character.h”
include “TutorialBaseCharacter.generated.h”
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnCharacterDead);
class UCameraComponent;
class USpringArmComponent;
class UHealthComponent;
class UTextRenderComponent;
class UWeaponComponent;
UCLASS()
class SHOOTERTUTORIAL_API ATutorialBaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
ATutorialBaseCharacter(const FObjectInitializer& ObjInit);
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
void SetPlayerColor(const FLinearColor& Color);
FOnCharacterDead CharacterDead;
/*
*/
TutorialBaseCharacter.cpp code where the broadcast() function is called:
void ATutorialBaseCharacter::OnDeath()
{
// PlayAnimMontage(DeathAnimation);
WeaponComponent->DestroyComponent();
GetCharacterMovement()->DisableMovement();
SetLifeSpan(LifeSpanOnDeath);
if (Controller)
{
Controller->ChangeState(NAME_Spectating);
}
WeaponComponent->StopFire();
CharacterDead.Broadcast(); //here
GetMesh()->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
GetMesh()->SetSimulatePhysics(true);
GetMesh()->SetMassOverrideInKg(NAME_None, CharacterMass);
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Ignore);
GetCapsuleComponent()->SetCapsuleSize(10.0f, 25.0f, false);
}
TutorialBaseWeapon.h code:
#pragma once
include “CoreMinimal.h”
include “GameFramework/Actor.h”
include “MyCoreTypes.h”
include “TutorialBaseWeapon.generated.h”
class ATutorialBaseCharacter;
UCLASS()
class SHOOTERTUTORIAL_API ATutorialBaseWeapon : public AActor
{
GENERATED_BODY()
public:
ATutorialBaseWeapon();
FOnCharacterDead CharacterDead;
/*
*/
TutorialBaseWeapon.cpp. This is where the function bind takes place:
// Game. All Rights Reserved.
include “Weapon/TutorialBaseWeapon.h”
include “Components/SkeletalMeshComponent.h”
include “DrawDebugHelpers.h”
include “Engine/World.h”
include “GameFramework/Character.h”
include “GameFramework/Controller.h”
include “NiagaraComponent.h”
include “NiagaraFunctionLibrary.h”
include “TimerManager.h”
include “TutorialUtils.h”
include “MyCoreTypes.h”
include “Player/HealthComponent.h”
include “Player/TutorialBaseCharacter.h”
include “Kismet/GameplayStatics.h”
DEFINE_LOG_CATEGORY_STATIC(LogBaseWeapon, All, All)
ATutorialBaseWeapon::ATutorialBaseWeapon()
{
PrimaryActorTick.bCanEverTick = false;
WeaponMesh = CreateDefaultSubobject(“Weapon Mesh”);
SetRootComponent(WeaponMesh);
}
void ATutorialBaseWeapon::BeginPlay()
{
Super::BeginPlay();
check(WeaponMesh);
checkf(DefaultAmmo.Bullets > 0, TEXT("Bullets count couldn't be less or equal zero"));
checkf(DefaultAmmo.Clips > 0, TEXT("Clips count couldn't be less or equal zero"));
CurrentAmmo = DefaultAmmo;
}
/**/
void ATutorialBaseWeapon::SetWeaponInvisible()
{
WeaponMesh->SetVisibility(false);
}
void ATutorialBaseWeapon::Initialize()
{
ATutorialBaseCharacter* BaseCharacter = Cast(GetOwner());
if (BaseCharacter)
{
BaseCharacter->CharacterDead.AddDynamic(this, &ATutorialBaseWeapon::SetWeaponInvisible);
}
}
Here is the actual code that worked fine before certain changes that have nothing to do with the delegate itself.
The changes that cause this error (without these changes everything works fine):
In the file AIRifleWeapon.cpp changing the line
HittenActor->TakeDamage(Damage, FDamageEvent{}, GetPlayerController(), this);
on
HittenActor->TakeDamage(Damage, FDamageEvent{}, GetController(), this);
and I describe this function
AController* AAIRifleWeapon::GetController() const
{
const auto Pawn = Cast(GetOwner());
return Pawn ? Pawn->GetController() : nullptr;
}
in the TutorialBaseWeapon.cpp file, I delete this code:
APlayerController* ATutorialBaseWeapon::GetPlayerController() const
{
const auto Player = Cast(GetOwner());
if (!Player) return nullptr;
return Player->GetController<APlayerController>();
}
line - const auto Controller = GetPlayerController();
change to
const auto Controller = Character->GetController();
on file TutorialRifleWeapon.cpp
changing line
HittenActor ->TakeDamage(Damage, FDamageEvent{}, GetPlayerController(), this);
on
HittenActor->TakeDamage(Damage, FDamageEvent{}, GetController(), this);
and describe the function
AController* ATutorialRifleWeapon::GetController() const
{
const auto Pawn = Cast(GetOwner());
return Pawn ? Pawn->GetController() : nullptr;
}
Also in two files: TutorialRifleWeapon.h and AAIRifleWeapon.h I declare the functions
AController* GetController() const;
These are all changes, as you can see, I don’t touch the delegate in any way, but after making this code - an error appears. What is the reason?