Call method when "using" it

Hi, maybe I’m doing this all wrong, if so, please correct me

I have a base class, that is being inherited by other classes.
The base class has a Use() method in it. This will most likely just be used for game state management.

The child classes are objects that have a Use() override method on them. The Use() method can do anything depending on what type the child class is (eg: radio will turn on, or book will open, or light switch will flick on and off)

so on Use, I’m able to detect the object I’m hitting, and what type of “Child” class it is


FHitResult HitResult = GetFirstPhysicsBodyInReach();
    AActor* ActorHit = HitResult.GetActor();
    if (ActorHit && ActorHit->IsA(AObjectBase::StaticClass())) {
        UE_LOG(LogTemp, Warning, TEXT("We should call the USE function now!"));
}

So My question is, within the IF block, what’s the best way to call the Use() method of the object I’m interacting with?

Remember, I could have MULTIPLE Objects, that each derive off the AObjectBase class.

Thanks in advance

Just call the Use() function on the hit actor. Thats it. The current code basically says: “I know that I hit an AObjectBase actor, meaning I can call Use() on it. I don’t know exactly what will happen, but it doesn’t matter to me, my only job is to start the interaction.”

I’ve tried that. doesn’t work

ue4.JPG

So far the only way I’ve been able to get it to work, is to cast it to the specific object type. So check what type of child object ActorHit is, cast it to that, and then call Use();
Gonna be a pain if I should have 1000+ different object types.

You have to cast to use a method from that interface/parent/child. The point of having everything inherit from your Use base class is you only have to cast to that base class. So if you have 1000 child classes that all inherit from AUseObject (or whatever), it’s still only one cast.



if (AUseObject* IsUsable = Cast<AUseObject>(AHitActor))
{
   IsUsable->Use();
}


If that works, I’m gonna kiss you… let me test it quick

Awesome… That works. Thank you.!!!

So purely casting it to the base object works. Thanks