Hi. I created Interface and Coin that implements it. But I feel like Im doing something wrong. Because I use interface. Then In this coin I cast value that I passed (Acharacter) and set variables inside of my character. Maybe I should just use OnOverlapEvent Cast to this character and just set value. I just made interface if there would be something else than coins for example heart (for healing). Heres my code:
// character
void APlatformer3dCharacter::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* Component, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if(IICollectItem* Interface = Cast<IICollectItem>(OtherActor))
{
Interface->OnPickUpItem_Implementation(this);
}
}
// coin
void AACoin::OnPickUpItem_Implementation(ACharacter* PlayerCharacter)
{
if (PlayerCharacter)
{
UE_LOG(LogTemp, Warning, TEXT("OnPickUpItem for character: %s"), *PlayerCharacter->GetClass()->GetName());
// Attempt to cast PlayerCharacter to APlatformer3dCharacter
if (APlatformer3dCharacter* Character = Cast<APlatformer3dCharacter>(PlayerCharacter))
{
// Update the Coins variable
Character->Coins += 10;
}
interface
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category= CollectItem)
void OnPickUpItem(ACharacter* PlayerCharacter);