Scenario: I have an actor, with a box collider added onto it. Then I added an actor component that gets the box collider and initializes it through code. I managed to make the FindComponentByClass to work, but im trying to get the box collider to bind to the actor component’s function. All that said, the only line that isnt working is the line with AddDynamic in it. I was wondering what the problem is and how it can be avoided in the future. Here is the .h of the actor component:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/BoxComponent.h"
#include "Components/ActorComponent.h"
#include "DisplayOnCollide.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class PLAYGROUND_3PS_API UDisplayOnCollide : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UDisplayOnCollide();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
void InitCollider();
void CheckForCollider();
UPROPERTY(EditAnywhere)
UBoxComponent* BoxCollider = nullptr;
UPROPERTY(EditAnywhere)
FString Message;
UFUNCTION()
void OnCollision(AActor* ThisCollider, AActor* OtherCollider);
};
And the .cpp :
// Fill out your copyright notice in the Description page of Project Settings.
#include "GameFramework/Actor.h"
#include "DisplayOnCollide.h"
// Sets default values for this component's properties
UDisplayOnCollide::UDisplayOnCollide()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UDisplayOnCollide::BeginPlay()
{
Super::BeginPlay();
//Collider
InitCollider();
CheckForCollider();
}
// Called every frame
void UDisplayOnCollide::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
void UDisplayOnCollide::InitCollider()
{
BoxCollider = GetOwner()->FindComponentByClass<UBoxComponent>();
BoxCollider->SetBoxExtent(FVector(100.0f, 200.0f, 100.0f), true);
BoxCollider->SetCollisionProfileName("Trigger");
BoxCollider->OnComponentBeginOverlap.AddDynamic(this, &UDisplayOnCollide::OnCollision);
}
void UDisplayOnCollide::CheckForCollider()
{
if (BoxCollider != nullptr)
{
GEngine->AddOnScreenDebugMessage(-1, 4.0f, FColor::Green, "BoxCollider Set!");
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 4.0f, FColor::Red, "BoxCollider NOT SET!");
}
}
void UDisplayOnCollide::OnCollision(AActor* ThisCollider, AActor* OtherCollider)
{
GEngine->AddOnScreenDebugMessage(-1, 4.0f, FColor::Green, "Checkpoint 1 Reached!");
}