3

I recently switched from unity and I wanted to know if there was a “getComponent” equivalent in UE4? I have an enemy script and I created a blueprint from that script and have added a widget with a progress bar to show the enemies health during combat. I have seen a lot of examples of how to do this in blueprint but if possible, I would like to just do it in code and let the percentage be calculated when it is needed to be, like after damage or something, rather than binding via blueprints.

I have tried using the getcomponentbyclass method but that throws an error because the component I want is not a child of the actor class.

Any help appreciated

AmberLynn
  • 31
  • 1
  • 4

1 Answers1

2

You need to call GetComponentByClass() on an Actor. If your Enemy is a Component, you can call getOwner() to retrieve the actor owning this component. You can then call GetComponentByClass() on this actor instance.

Raildex
  • 3,406
  • 1
  • 18
  • 42
  • I tried to upvote this but my reputation is too low so I’ll just say thank you for the clear answer. – AmberLynn Jan 21 '20 at 15:15
  • 1
    To add to that, you can also use template method [FindComponentByClass](https://docs.unrealengine.com/en-US/API/Runtime/Engine/GameFramework/AActor/FindComponentByClass/2/index.html) which will automatically cast to the type that you requested. – Max Play Jan 22 '20 at 22:57