Announcement

Collapse
No announcement yet.

How to use UE4 get component by class to get a collision component attached via BP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to use UE4 get component by class to get a collision component attached via BP

    Hey guys sorry if this comes across as a stupid question, Im coming over from Unity and am getting confused on UE4's version of GetComponent.

    I have a sphere collider component attached to a projectile blueprint that I am trying to access via c++. However when I try to do so, the editor crashes.


    This is how I am trying to get a refrence to this existing BP component via C++

    .h
    Code:
    class USphereComponent* Collision;
    .cpp
    Code:
    Collision = this->GetOwner()->FindComponentByClass<USphereComponent>();
    Below you can see the collision component attached.

    Click image for larger version

Name:	Untitled.png
Views:	45
Size:	16.3 KB
ID:	1782704


    If anyone could explain to me why this would cause a crash and a potentiol soloution I can use going forward for these types of scenarios I would be very greatful.

    #2
    this-> is redundant as UE4 doesn't allow shadowing (there is exceptions but generally you don't need it);

    You never stated where you are trying to get the component. If its in the actor then simply:

    Code:
    Collision = FindComponentByClass<USphereComponent>();
    is enough to get the sphere component.

    Reason your crashing is probably cause you are galling GetOwner() when GetOwner is invalid.

    Be warned you CAN NOT use FindComponent or GetComponent inside the Constructor. The earliest place is PostInitializeComponents or BeginPlay.
    Last edited by TheKaosSpectrum; 06-30-2020, 11:31 AM.
    Game Programmer - Working for a Indie Studio.

    Visit my blog: TheGamesDev

    InterKaos Games

    PC Specs:

    Comment


      #3
      Thanks, that works perfectly!

      Comment

      Working...
      X