How to call function on an Actor that was Hit

Hello,

I managed to detect when my main player character hits an enemy character in my scene (via collision). However, now I’m trying to call a function (from a base class) on this enemy character, and I can’t manage to figure out the syntax required.

Say the actor that was hit is the “OtherActor” in the OnHit function. How do I simply call a public function on the “OtherActor”? I created my own custom “Character” class and added a variable and a function to that class. Then I made this custom class the parent of the enemy character’s Blueprint. It is now the “parent” class of my enemy character’s regular “Blueprint”. And I simply want to call a function from that class when the aforementioned enemy character gets hit.

Here is the hit function I’m using:


void UMainCharacterController1::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
 // Other Actor is the actor that triggered the event. Check that is not ourself. 
 if ((OtherActor != nullptr) && (OtherActor != mainCharacterActor))
 {
  if (OtherActor->ActorHasTag("Enemy1"))
  {
   // Turn off the light 
   // PointLight->SetVisibility(false);
   UE_LOG(LogTemp, Warning, TEXT("I collided with something!"));

   // * How do I access a function on the actor that was hit? *
   //OtherActor->
  }
 }
}

Thanks for any info.

Cast it to your custom class, then call the method:


if (AYourCharacter* Character = Cast<AYourCharacter>(OtherActor))
{
    Character->Stuff();
}

Hi Zeblote,

Thanks for the response! For some reason the Visual Studio compiler is saying my custom class is undefined, as if it doesn’t exist or it can’t see it properly. My custom class files are in the source folder in my project, and they’re in the Visual Studio solution itself as well.

I was also able to parent my Enemy character’s blueprint to my custom class. So, I don’t know why the compiler can’t see it as a class. I can’t add the header file of my custom class to the #include of the file I’m trying to call it from either. I’m not sure why yet.

Here is the header file of my custom class, in case that might help:


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyMedWarCharacterClass1.generated.h"

UCLASS()
class CPLUSPLUSTEST2_API AMyMedWarCharacterClass1 : public ACharacter
{
 GENERATED_BODY()

public:
 // Sets default values for this character's properties
 AMyMedWarCharacterClass1();

protected:
 // Called when the game starts or when spawned
 virtual void BeginPlay() override;

public: 
 // Called every frame
 virtual void Tick(float DeltaTime) override;

 // Called to bind functionality to input
 virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;


 UFUNCTION()
 virtual void SetGoRagdollTrue();


 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = MedWarGeneralVars1)
 bool shouldGoRagdoll1;




};

Nevermind I got it work! :slight_smile: Thank you.

I found out I had to include the header file of my game project (my game project’s name / api header file). Then I was able to include my custom classes header file after that. And then it recognized my custom class in the compiler.

Your code works of course.

Thanks again.