Download

How to access custom actor components through C++? (And general practice).

I have one custom component on my character called “Use”, which simply line traces out and returns which type of
collision channel it hit (ECC_PhysicsBody, ECC_WorldStatic…).

Say I have 2 other custom actor components on my character, one that picks up objects with a physics handle
and one that tells doors and buttons etc. to activate when pushed.

At the moment, I have something like this for determining which type of object was hit:



auto HitResult = TraceRayCast();

    if (HitResult.GetActor())
    {
        auto CollisionType = HitResult.GetComponent()->GetCollisionObjectType();

        if (CollisionType == ECC_PhysicsBody)
        {
            UE_LOG(LogTemp, Warning, TEXT("Physics Object Hit."));

            // Make grabbing component do its job

        }
        else if (CollisionType == ECC_GameTraceChannel1)
        {
            UE_LOG(LogTemp, Warning, TEXT("Door or button hit.")); 
// Activate whatever was hit (Door, button)
 
        }

    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("Nothing Hit."));
    }
}

How can I tell the other components on the actor to do their job from here?
If the Door had a “Open” component, How could I let my Use function tell it
its time to open?

I could be going at this very wrong I do not have any real programming training
but I want to know what you guys think.

Thank you!!!

You can find a component on an actor like such:



if (MyCustomComponent* CustomComponent = HitResult.GetActor()->FindComponentByClass<MyCustomComponent>())
{
   // The actor had my custom component, do something with it.
   CustomComponent->DoStuff();
}


That was my initial thought but visual studio doesnt know what my custom component is called and always fails compliation. There must be a way I can create a reference to the custom component I made in unreal. Or maybe define it somewhere?

You need to include it in whatever files you are using it.

So,



#include "MyCustomComponent.h"


haha! I was really overthinking it thank you