x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

make Actor Component respond to TakeDamage() on parent Actor

So basically what I'm trying to do is to create a single unified ActorComponent that will handle health and damage for any actor it is attached to.

I want to make it so that by simply attaching my component to some Actor, that actor now can have HP and shields and whatever.

However I ran into a problem of understanding here, a bit.

So basically, when something deals damage to my Actor, than on that actory TakeDamage() function is called. I can overload this function on the actor and handle damage there. But I wanted to make it so that as long my ActorComponent is attached to the actor, then whenever actor takes damage from anything - a function on the component is called and handles the situation. Without me having to add any code to the actor itself.

How can I do that? Someone said that I should use delegates, but I would still need to add a delegate to the actor, so basically the purpose of not having to add any code is defeated.

Thanks in advance for responses.

Product Version: UE 4.19
Tags:
more ▼

asked Dec 10 '18 at 05:12 AM in C++ Programming

avatar image

kozmyukv
1 2

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

It is indeed done through delegates.

In YourComponent.h you add a function:

 UFUNCTION()
 void TakeDamage(AActor* DamagedActor, float Damage, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser);

Note that it can be called however you want. You obviously implement this function in your .cpp file. Note also that this call differs slightly from a standard TakeDamage() that you would overload on your actor. It has an additional parameter at the front - AActor* DamagedActor

Also in the initialization block of your .cpp file you write this:

 GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UYourComponent::TakeDamage);

This will subscribe your TakeDamage function to an event that occurs on the owner Actor.

more ▼

answered Dec 11 '18 at 05:14 AM

avatar image

kozmyukv
1 2

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question