Hello fellow friends of problems!
Now, I have been using the unreal engine for about 2 weeks. I am experienced in programming with c++ and thought “Hey, this is a great engine to create a game with”, but now I am having a huge problem which I just simply can’t understand.
This is the .cpp
#include "Player_HitBoxComp.h"
#include "ZA_WARUDOCharacter.h"
#include "Runtime/Engine/Classes/Engine/Engine.h"
UPlayer_HitBoxComp::UPlayer_HitBoxComp()
{
SetCollisionProfileName(TEXT("Hitbox"));
OnComponentBeginOverlap.AddDynamic(this, &UPlayer_HitBoxComp::OnOverlapBeginHitBox);
OnComponentEndOverlap.AddDynamic(this, &UPlayer_HitBoxComp::OnOverlapEndHitBox);
}
//Collision handling
void UPlayer_HitBoxComp::OnOverlapBeginHitBox(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (GetWorld())
{
AZA_WARUDOCharacter* const PlayerRef = Cast<AZA_WARUDOCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter());
PlayerRef->OnOverlapBeginHitBox(OverlappedComp, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
}
}
void UPlayer_HitBoxComp::OnOverlapEndHitBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (GetWorld())
{
AZA_WARUDOCharacter* const PlayerRef = Cast<AZA_WARUDOCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter());
PlayerRef->OnOverlapEndHitBox(OverlappedComp, OtherActor, OtherComp, OtherBodyIndex);
}
}
And this is the .h
#pragma once
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "Player_HitBoxComp.generated.h"
/**
*
*/
UCLASS(meta = (BlueprintSpawnableComponent))
class ZA_WARUDO_API UPlayer_HitBoxComp : public UBoxComponent
{
GENERATED_BODY()
UPlayer_HitBoxComp();
protected:
UFUNCTION()
void OnOverlapBeginHitBox(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnOverlapEndHitBox(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};
Now, what this is for is obvious. It’s a collision component which I am using as a hitbox for the playercharacter’s attacks.
Problem is, that as soon as I turn on overlapping events, the blueprint I have created(which ofc. derives from the Character) to effectively add and move the component crashes on startup.
If I activate it and go into the game, the hitbox itself works, just the blueprint isnt editable anymore.
The line that causes all this is:
AZA_WARUDOCharacter* const PlayerRef = Cast<AZA_WARUDOCharacter>(GetWorld()->GetFirstPlayerController()->GetCharacter());
But I dont forking understand why.
But perhaps you, more or less random reader, know why and can explain me why this is happening.
I appreciate your help.
May the force be with you.