AApple::AApple()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Tags.Add("Apple");
BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));
BoxComponent->InitBoxExtent(FVector(50));
BoxComponent->SetCollisionProfileName("OverlapAll");
BoxComponent->SetGenerateOverlapEvents(true);
RootComponent = BoxComponent;
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
MeshComponent->SetCollisionProfileName("NoCollision");
MeshComponent->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AApple::BeginPlay()
{
Super::BeginPlay();
if (BoxComponent)
{
GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Add");
BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AApple::OnOverlap);
}
GridSubsystem = GetWorld()->GetSubsystem<UGridSubsystem>();
if (GridSubsystem)
{
FIntVector GridSize = GridSubsystem->GetMatrix();
FIntVector RandomGridPos;
do
{
RandomGridPos = FIntVector(FMath::RandRange(0, GridSize.X - 1), FMath::RandRange(0, GridSize.Y - 1), FMath::RandRange(0, GridSize.Z - 1));
} while (CurrentGridPosition == RandomGridPos || GridSubsystem->GetTileState(RandomGridPos) != TileState::Unoccupied);
FVector NewLocation;
GridSubsystem->GetTilePosition(RandomGridPos, NewLocation);
CurrentGridPosition = RandomGridPos;
SetActorLocation(NewLocation);
}
}
void AApple::OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 60, FColor::Cyan, "Overlap");
if (!OtherActor->ActorHasTag("Head")) return;
if (GridSubsystem->GetOccupiedCount() == GridSubsystem->GetCount()) SetActorLocation(FVector(-500, -500, 0));
FIntVector GridSize = GridSubsystem->GetMatrix();
FIntVector RandomGridPos;
do
{
RandomGridPos = FIntVector(FMath::RandRange(0, GridSize.X - 1), FMath::RandRange(0, GridSize.Y - 1), FMath::RandRange(0, GridSize.Z - 1));
} while (CurrentGridPosition == RandomGridPos || GridSubsystem->GetTileState(RandomGridPos) != TileState::Unoccupied);
FVector NewLocation;
GridSubsystem->GetTilePosition(RandomGridPos, NewLocation);
CurrentGridPosition = RandomGridPos;
SetActorLocation(NewLocation);
}
Here it is.
The thing is the AddOnScreenDebugMessage at the beginning of OnOverlap is not getting called at all.