Dear experts,
I am a beginner and trying to set up a 3D Jump’n’Run game.
I have a character that runs through the map and is collecting actors such as cones, cubes, etc.
In my “ConesCharacter.cpp” I setup an “OnOverlapBegin” method (I followed https://unrealcpp.com/on-overlap-begin). So my Character is running around, overlapping actors which
disappear and a counter counts and a HUD shows the counter value - please see attached screenshot.
void AConesCharacter::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//let's get an instance of MyGameInstance
UMyGameInstance *MGI = Cast<UMyGameInstance>(GetGameInstance());
if (OtherActor && (OtherActor != this) && OtherComp){
FName Name = OtherActor->GetClass()->GetFName();
//we make sure the "ConeCounter" value is equal to what is in MyGameInstance; otherwise
//when we switch levels "ConeCounter" starts from 0 again
ConeCounter = MGI->InterLevelConeCountValue;
if (Name == "Cone_BP_C") {
ConeCounter++;
}
//update the user widget with "ConeCounter" value
if (LocTextControl != nullptr) {
LocTextControl->SetText(FText::FromString(FString::FromInt(this->GetConeCounterValue())));
}
//let's save the new "ConeCounter" value to our MyGameInstance
if (MGI)
{
MGI->InterLevelConeCountValue = ConeCounter;
UE_LOG(LogTemp, Warning, TEXT("cone count value: %i"), MGI->InterLevelConeCountValue);
}
//now if our character overlaps the power up mesh (yellow key) we switch to the next level
if (Name == "Pl_PowerUp_03_C") {
SwapLevel();
}
}
}
You see in the overlap method there is too much going on: For example I check if I am overlapping a cone
if (Name == "Cone_BP_C")
or another object
if (Name == "Pl_PowerUp_03_C")
. I update the user widget counter:
if (LocTextControl != nullptr) {
LocTextControl->SetText(FText::FromString(FString::FromInt(this->GetConeCounterValue())));
}
Question:
How do I check what which kind of actor is overlapped? Would an observer pattern help? Do I make the overlapped actors delegates or my ConeCharacter a delegate? Do I update the
User widget counter from the PlayerController or leave it in the Overlap method of the ConeCharacter?
Thank you for any useful information.
Best regards, Peter