我用c++写了一个碰撞,但是它一启动就崩溃了。下面是代码
.h
UCLASS()
class DIABLOCPP_API AHIT1 : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AHIT1();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class USphereComponent* SphereComp;
UFUNCTION()
void OnActorOverlap(class UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
.cpp
// Sets default values
AHIT1::AHIT1()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SphereComp = CreateDefaultSubobject<USphereComponent>("CollisionSphere");
SphereComp->SetSphereRadius(100.0f);
SphereComp->SetCollisionProfileName(TEXT("Projectile"));
RootComponent = SphereComp;
}
// Called when the game starts or when spawned
void AHIT1::BeginPlay()
{
Super::BeginPlay();
SphereComp->OnComponentBeginOverlap.AddDynamic(this, &AHIT1::OnActorOverlap);
}
// Called every frame
void AHIT1::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AHIT1::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AHIT1::OnActorOverlap(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!"));
}
呼叫线路时崩溃:SphereComp->OnComponentBeginOverlap.AddDynamic(this, &AHIT1::OnActorOverlap);
我试图在构造函数中绑定一个碰撞,但是即使没有崩溃,它也不起作用。我不明白我的错误,在其他更复杂的例子中,一切都正常。
1条答案
按热度按时间0sgqnhkj1#
类型转换的
CreateDefaultSubobject
应该有TEXT
类型arg。变更:
对此:
之后,您的SphereComp将被创建,并且不会是nullptr(我希望如此,呵呵)