Check if overlapping actor is player character

Hi! I’ve recently stared learning UE and I am currently working on PowerUp system. This is the overlapping function for Damage PowerUp class:

void ADamagePowerUp::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{	
	Destroy();
}

I would like to check if the overlapping actor is player character (class APlayerCharacter) to set new value of it’s “Damage” variable. How can I do that?

what you will need to do is “try to” Cast the OtherActor to the appropriate class, so that you can determine the type, and/or access the correct member functions/variables

in this example

void ADamagePowerUp::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
      // try to cast the OtherActor to MyCharacter
      if(AMyCharacter* Character = Cast<AMyCharacter>(OtherActor))
      {
          // do things based on the OtherActor being of type AMyCharacter here
      }
      // the Other Actor was "not" of type AMyCharacter
      else
      {
          // do things here for if it is "not" AMyCharacter, or leave it blank 
          // or omit the else block your choice
      }
}

you can replace the AMyCharacter with the appropriate Class name.
it is “safe” to call member functions/variables within the if-block and your IDE “should” prevent you from accessing what I called Character outside of it because I have bound the variable to the scope of the If block.
you will also need to #include the appropriate classes .h (my case AMyCharacter so MyCharacter.h) into the .cpp file of your ADamagePowerUp

Everything works as I wished. Thank you very much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.