Looking for general feedback as well as specific feedback on event handling. My 2 delegates, OnTriggerMeshBeginOverlap and OnTriggerMeshEndOverlap, are just one liners that call UpdateIsActivatedState(). UpdateIsActivatedState() is where all the logic got moved to. If I could eliminate the two delegates and just call UpdateIsActivatedState() that would be more readable. However, the two delegates have different signatures and I don’t think I can overload UFUNCTIONS. Let me know if you have thoughts. Additionally, I modified some of the logic at the end, Let me know if it looks more readable.
// .h
/**
* Modifies IsActive based on actors tagged with "TriggerActor"
* @param bIsActivatedLocal The requested activation state
*/
UFUNCTION()
void UpdateIsActivatedState(bool bIsActivatedLocal);
/** just calls UpdateIsActivatedState() */
UFUNCTION()
void OnTriggerMeshBeginOverlap(UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex,
bool bFromSweep,
const FHitResult& SweepResult
){ UpdateIsActivatedState(true); };
/** just calls UpdateIsActivatedState() */
UFUNCTION()
void OnTriggerMeshEndOverlap(UPrimitiveComponent* OverlappedComponent,
AActor* OtherActor,
UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex
){ UpdateIsActivatedState(false); };
// .cpp
APressurePlate::APressurePlate()
{
TriggerMesh->OnComponentBeginOverlap.AddDynamic(this, &APressurePlate::OnTriggerMeshBeginOverlap);
TriggerMesh->OnComponentEndOverlap.AddDynamic(this, &APressurePlate::OnTriggerMeshEndOverlap);
}
void APressurePlate::UpdateIsActivatedState(const bool bIsActivatedLocal)
{
// is there an actor tagged "TriggerActor" on the pressure plate? Same logic as Kaan.
// Update IsActivated. A little different than Kaan.
if (TriggerActor && !bIsActivated)
{
bIsActivated = true;
OnActivated.Broadcast();
UE_LOG(LogOfPressurePlate, Log, TEXT("%s is now Active"), *GetActorNameOrLabel());
}
else if (!TriggerActor && bIsActivated)
{
bIsActivated = false;
OnDeactivated.Broadcast();
UE_LOG(LogOfPressurePlate, Log, TEXT("%s is now NOT Active"), *GetActorNameOrLabel());
}
The original // Update IsActivated
if (TriggerActor)
{
if (!Activated)
{
Activated = true;
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::White, TEXT("Activated"));
OnActivated.Broadcast();
}
}
else
{
if (Activated)
{
Activated = false;
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::White, TEXT("Deactivated"));
OnDeactivated.Broadcast();
}
}