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"

OnTakeDamage access violation on DamageEvent

Hi
I am adding a delegate to OnTakeDamage event. I could not find anywhere how to add raw delegates to events so this is how I did it (I am calling this in component's InitialiseComponent method):

 TScriptDelegate<FWeakObjectPtr> delegate;
 delegate.BindUFunction(this,FName(TEXT("TakeDamage")));
 GetOwner()->OnTakeAnyDamage.Add(delegate));

This is declaration of the TakeDamage method:

 UFUNCTION()
 float TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser);

I have there UFUNCTION just to be able to use it to create the delegate. This is working fine without any problems, if I don't use DamageEvent in the method. But if I try to use DamageEvent, I have "Access violation executing location". Just simply calling this, is causing the crash:

 auto typeid = DamageEvent.GetTypeID();

Another question, beside the obvious why it is crashing, would be how to bind raw functions (methods) to delegates (in many places there are answers like BindRaw, AddDynamic, but these are not present in OnTakeDamage event).

Thanks

Anicka

Product Version: Not Selected
Tags:
more ▼

asked Apr 14 '15 at 09:26 PM in C++ Programming

avatar image

AnickaBurova
11 1 2 5

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

1 answer: sort voted first

Well, I have found out what is the problem. I am doing it entirely wrong.
First, the delegates, at least for multicast, should obviously not return a value. It is an event so there is no way.
The other problem is, the OnTakeDamage declaration is different than my TakeDamage method. It should not have FDamageEvent& argument, but const UDamageType pointer. The problem is, the method I used to add the delegate using TScriptDelegate, is not checking for arguments (so adding it was passing without any problems). And when broadcasting, it is passing arguments as pointers, which are then reintrepreted as they are in the delegate method. So UDamageType* is casted to FDamageEvent&, and this is failing when I want to access it in runtime.
So that is blunt explanation why I have the access violation crash.

Finally I have found the way how to do it properly. AddDynamic is not a method but it is a macro, that is why I could not find it. So jush use OnTakeDamage.AddDynamic(this,&ClassName::TakeDamage);

The delegate method declaration now looks like this:

 void TakeDamage(float Damage, const UDamageType* DamageType, AController* EventInstigator, AActor* DamageCauser);



more ▼

answered Apr 15 '15 at 10:45 PM

avatar image

AnickaBurova
11 1 2 5

(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