Hi, Iโm trying to pop up a UI that says I won if the player killed the NPC.
But now I have a problem. Crash has occurred.
Let me show you the crash first.
LoginId:c94d24654bb053080c17c2ad5040f05a
EpicAccountId:fccedefcd3d646a6a9f2c9f009701903Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000458
UnrealEditor_LifeOfLuck!UE::CoreUObject::Private::ResolveObjectHandleNoRead() [D:\UE_5.3\Engine\Source\Runtime\CoreUObject\Public\UObject\ObjectHandle.h:256]
UnrealEditor_LifeOfLuck!AOLGameMode::DeadCheck() [D:\Project\LifeOfLuck\Source\LifeOfLuck\GameMode\OLGameMode.cpp:35]
UnrealEditor_LifeOfLuck!AOLCharacterBase::TakeDamage() [D:\Project\LifeOfLuck\Source\LifeOfLuck\Character\OLCharacterBase.cpp:182]
UnrealEditor_LifeOfLuck!AOLCharacterBase::AttackHitCheck() [D:\Project\LifeOfLuck\Source\LifeOfLuck\Character\OLCharacterBase.cpp:153]
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_UnrealEd
UnrealEditor_UnrealEd
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll
And now Iโm going to show you my code.
float AOLCharacterBase::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
Super::TakeDamage(DamageAmount, DamageEvent, EventInstigator, DamageCauser);
Hp = FMath::Clamp(Hp - DamageAmount, 0.0f, MaxHp); // ์ฒด๋ ฅ ์
๋ฐ์ดํธ
UE_LOG(LogTemp, Warning, TEXT("Damage = %f"), DamageAmount);
UE_LOG(LogTemp, Warning, TEXT("Hp = %f"), Hp);
// ์ฒด๋ ฅ์ด 0 ์ดํ์ธ ๊ฒฝ์ฐ
if (Hp <= 0.0f)
{
AOLGameMode* GameMode = Cast<AOLGameMode>(GetWorld()->GetAuthGameMode()); // ๊ฒ์๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ
if (GameMode)
{
GameMode->DeadCheck(this); // ์ด๋ค ์บ๋ฆญํฐ๊ฐ ์ฃฝ์๋์ง ํ์ธ
}
Dead(); // ์ฃฝ์์ผ๋ ํจ์ ํธ์ถ
UE_LOG(LogTemp, Error, TEXT("Dead!"));
}
return DamageAmount;
}
AOLGameMode::AOLGameMode()
{
NPCs = 0;
}
void AOLGameMode::BeginPlay()
{
Super::BeginPlay();
GameStart();
}
void AOLGameMode::GameStart()
{
NPCs = NPCCount(); // ์๋์ ์๋ NPC ์ ์ด๊ธฐํ
UE_LOG(LogTemp, Warning, TEXT("NPC is %d"), NPCs);
}
void AOLGameMode::DeadCheck(ACharacter* Character)
{
// ์บ๋ฆญํฐ๋ก ํ์ธ
Player = Cast<AOLCharacterPlayer>(Character);
NPC = Cast<AOLNPCBasic>(Character);
// ํ๋ ์ด์ด๊ฐ ์ฃฝ์ ๊ฒฝ์ฐ
if (Character == Player)
{
AOLPlayerController* PlayerController = Cast<AOLPlayerController>(Player->GetController()); // ํ๋ ์ด์ด์ ์ปจํธ๋กค๋ฌ ๊ฐ์ ธ์ค๊ธฐ
if (PlayerController)
{
PlayerController->GameOver();
}
}
// NPC๊ฐ ์ฃฝ์ ๊ฒฝ์ฐ
else if (Character == NPC)
{
NPCs--; // NPC๊ฐ ์ฃฝ์ผ๋ฉด 1 ๋นผ์ฃผ๊ธฐ(NPC ์ ์ฐจ๊ฐ)
AOLPlayerController* PlayerController = Cast<AOLPlayerController>(Player->GetController()); // ํ๋ ์ด์ด์ ์ปจํธ๋กค๋ฌ ๊ฐ์ ธ์ค๊ธฐ
// ์๋์ ์๋ ๋ชจ๋ NPC๊ฐ ์ฃฝ์ ๊ฒฝ์ฐ
if (NPCs == 0)
{
if (PlayerController != nullptr)
{
PlayerController->GameClear();
}
else
{
UE_LOG(LogTemp, Error, TEXT("No PlayerController"));
}
}
}
}
int32 AOLGameMode::NPCCount()
{
TArray<AActor*> AllNPC; // ์๋์ ์๋ ๋ชจ๋ NPC๋ฅผ ๋ด์ ๋ฐฐ์ด
// ์๋์ NPC ํด๋์ค๋ก ๋์ด์๋ ๋ชจ๋ ์กํฐ๋ค์ ๋ฐฐ์ด์ ๋ฃ์ด์ฃผ๊ธฐ
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AOLNPCBasic::StaticClass(), AllNPC);
UE_LOG(LogTemp, Display, TEXT("ALLNPC: %d"), AllNPC.Num());
return AllNPC.Num(); // ์๋์ ์๋ ๋ชจ๋ NPC ์ ๋ฐํ
}
The problem with this code now is that even if there are multiple NPCs, just kill the NPCs, the crash like above occurs.
I left one NPC in the world now. But when the NPCโs stamina goes to zero, it suddenly crashes. Whatโs the problem?
For your information, it works fine when the player dies. Instead, the problem only happens when the player kills the NPC.
And I declared NPC as UPROPERTY. But when I check it with Unreal Engine, NPCs are fixed to 0, and the value does not change.
Please help me