How do you know which arguments to pass in Hit Events?

I was looking through the documentation for the UE OnComponentHit and I understand you throw in a callback function there as when something is hit data is sent to that function. My question is regarding how do you know what paramets will be in that callback function? for example, we have OnHit(parameter, parameter, parameter)

where are you getting these arguments? I couldn’t find it in the documentation anywhere (maybe i’m overlooking it or looking in the wrong place)

I’d like to know this as there may be times in the future i’d have to dig into the documentation myself to find answers I can’t find in a video.

Btw. Great videos and thanks.

1 Like

Hi,

I would realy like to know if there is a better way than the one i am currently using. I search the solution for the OnComponentHit.

Sample From VS:
grafik

This gives me the defition :

Now you can look at the Signature(In this case FComponentHitSignature):

Here is the Code:

DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( FComponentHitSignature, UPrimitiveComponent, OnComponentHit, UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit );

From here you can Extract the needed Parameters.

It is a bit better with Rider cause the Intelisense gives better hints and it searches faster. Maybe I am to
used to Java Programming to be happy with this workflow…

1 Like

@cazgamedev I had basically the same question, since the documentation doesn’t tell much here. Seems like going to definitions inside the engine codebase is quite frequently required. Seems like devs have to get used to that.
Since the event handling seems to be a very useful system it may be worth learning all that, even it’s a little hidden.
Thanks to @benderbiber for the answer.

Came here for the same reason. A teacher shouldn’t use “magic” stuff. Don’t tell me the OnHit function needs this, this and this, without any explanation, explain me how to know OnHit function needs those arguments.

If he has to search the definitions inside engine code, show us the process, so when we face something similar, we know how to do it.

I was so happy with this new version of ToonTanks until this part, when suddenly we go back to the same problem of previous version of ToonTannks where the teacher just says loud what he writes without explaining how.

1 Like

@Morderkainer Since I started the course just a week ago I wasn’t aware of the hisotry of it and just checked a few of your posts about ToonTanks. And that it was updated etc.

So basically I only did the new version. I understood everything until this lecture, too. And then I just hit a wall, I kind of expected this TBH and thought its just me lacking basic C++ knowledge about delegates and events.

I did 2-3 additional sections of TonnTanks but I’m still stuck on reading about delegates / events and Actor communication in general on the UE docs.

Is this topic picked up later in the course / explained once more?

@Tarvish don’t know, I’ve been searching and trying to learn about delegates and AddDynamic.

To answer some questions.

The function you are adding to the delegate needs the specific signature because when the event happens the delegate will be calling the functions with arguments of those specific types e.g.

Func(1, 20.f, 'h');

So in this example if the function doesn’t have int, float, char as parameters then there’s issues.

A minimal code example

Compiler Explorer

The broadcasts would be in engine code and would be called when the component hits something.
I should probably mention that this isn’t how Unreal implements this but the general idea is the same.


@sampattuzzi this is something to keep in mind for the remaster. Do you want me to create an issue on notion?

Problems with the lesson:
1- Nobody explain us what is a Delegate, how to search for em (because it is something to search in the code, not documentation), how it looks, how to understand it and how to use it.
2- Nobody explain us what is a bind, specially BindDynamic, what it does, how to use it.

The lesson is basically: we need to create a bind to OncomponentHit with AddDynamic and create an OnHit function that takes these parameters (and proceed to write em while saying em).

Imagine teaching maths to a kid and say “we need to divide 20 between 5, we write 20:5 and the answer is 4”, without teaching him how to divide or what is a division. The kid would like “why 4? where that 4 came from? how do you know it is 4 and not 2 o 10?”. So this is how I look with Delegates and BindDynamic after this lesson.

Same happens with Health Component lesson: “we are going to add a callback function, now this can be void and we can call it anything we like, and because we are binding it to a delegate, it has to have the correct input parameters appropriate for that delegate. Now for functions bound to the ontakeanydamage delegate, the input parameters list has to be like that follows…” and proceed to write em while reading em.

Same problem, how do we know those parameters with that explanation? how do we find that delegate?

I was able to find this for OnTakeAnyDamage thanks to @benderbiber post and a discord user, but not thanks to the video lesson. And still don’t really know how it works and why we skip the first 3 parameters “FTakeAnyDamageSignature, AActor, OnTakeAnyDamage”.

DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( FTakeAnyDamageSignature, AActor, OnTakeAnyDamage, AActor*, DamagedActor, float, Damage, const class UDamageType*, DamageType, class AController*, InstigatedBy, AActor*, DamageCauser );

All this stuff should stay inside the video lessons, not on discord and the forum, because I was lucky a discord user took his time to explain me some stuff, but the next student maybe isn’t that lucky.

1 Like

All good points and I agree, however the course is being remastered soon™ so I’m not sure how Sam plans to address this (hence the tag).

Well, you don’t need to. Just like you don’t need to know how the property system works in order to use it. You don’t need to know what that macro is doing beyond declaring a multi-cast delegate.

That requires knowing the inner details of that macro. This is more of a failure of the documentation than anything, you shouldn’t really need to know that that macro even exists.

For what it’s worth the macro usage

DECLARE_DYNAMIC_MULTICAST_SPARSE_DELEGATE_FiveParams( FComponentHitSignature, UPrimitiveComponent, OnComponentHit, UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit );

Expands to

class FComponentHitSignature_MCSignature : public TBaseDynamicMulticastDelegate<FWeakObjectPtr, void, UPrimitiveComponent*, AActor*, UPrimitiveComponent*, FVector, const FHitResult&> 
{ 
public: 
    FComponentHitSignature_MCSignature() { } 
    explicit FComponentHitSignature_MCSignature( const TMulticastScriptDelegate<>& InMulticastScriptDelegate ) : TBaseDynamicMulticastDelegate<FWeakObjectPtr, void, UPrimitiveComponent*, AActor*, UPrimitiveComponent*, FVector, const FHitResult&>( InMulticastScriptDelegate ) { } 
    void Broadcast(UPrimitiveComponent* InParam1, AActor* InParam2, UPrimitiveComponent* InParam3, FVector InParam4, const FHitResult& InParam5 ) const 
    { 
        FComponentHitSignature_DelegateWrapper(*this, InParam1, InParam2, InParam3, InParam4, InParam5 ); 
    } 
}; 

struct FComponentHitSignatureInfoGetter
{ 
    static const char* GetDelegateName() { return "OnComponentHit"; } 
    template<typename T> static size_t GetDelegateOffset() 
    { 
        return offsetof(T, OnComponentHit); 
    } 
}; 


struct FComponentHitSignature : public TSparseDynamicDelegate<FComponentHitSignature_MCSignature, UPrimitiveComponent, FComponentHitSignatureInfoGetter> 
{ 

};;

The first argument names the class/structs.
The second argument is part of inheriting from TSparseDynamicDelegate at the end. Looking at that class for the first time, the argument is for the owning class

/** Sparse version of TBaseDynamicDelegate */
template <typename MulticastDelegate, typename OwningClass, typename DelegateInfoClass>
struct TSparseDynamicDelegate : public FSparseDelegate

No clue about the inner workings of that class. Though I don’t have to, because it’s not important to be able to use it.
The third argument names the delegate name as seen in FComponentHitSignatureInfoGetter.

The rest are the parameter types and the parameter names although the names are seemingly stripped out by the macro so are only there for readers.

1 Like

@DanM the UE Delegate docs explain that declaring own deleagates is also possible. Maybe it would make sense to implement a custom version instead of the shown built in damage delegates.
While I do understand showing the built in version, which I assume is the default way in productive development, the learn effect implementing it “by hand” would potentially be greater.

So in summary: OnComponentHit is used which shows how to used built in delegates and on the other hand a damage delegate is implemented in the lecture, maybe this gives a better picture of the system.

Not sure if this is even feasible within UE or the boundaries of the course but I leave this here as feedback. :slight_smile:

That was going something I was thinking about suggesting but I think that could also alienate a fair number of people as that’s a bit more involved than just understanding the gist of “adding your function to a list of invocations that will happen when the component hits something” and just black boxing it.

I was thinking for the section after could find a way to fit in creating your own to make it less mysterious would probably be better.

1 Like

I think the best way to teach this would be with something like:
1- Explain what is a delegate. Like something simple as, a delegate is like having a radio station that sends a signal each time something happens.
2- Explain what is AddDynamic. Like, using it makes our receiver (function) listen to that radio station and do something when receives the signal.
3- Explain why we need OnComponentHit, and that it is a delegate and we need to create our version and make it receive it signal when is triggered.
4- Search in UE4 documentation web and show that it doesn’t help us.
5- Show how to search in the code for the delegate.
6- Explain the structure of a delegate, like the FiveParams things, and the arguments are in pairs separated by coma with “type, name”. (and maybe more info, like what are the 3 first arguments we don’t need to create our version).
7- Make the AddDynamic to OnHit function.
8- ¿Challenge the student to try to make the OnHit function?

Later in the course:
9- Make our own simple delegate step by step and use it. ¿maybe to handle enemy turret death? or maybe as an extra to change the color or change the fire rate of all the enemy turrets when we press a key, don’t know something simple just to show how to make and use our own delegates.

I think this structure would make em really clear and easy to understand, so in the future if we need to use some UE4 premade delegates, we will know how to use em and how to make our own delegates.

Sorry if my example of radio station and receiver isn’t accurate or the best example, it is more or less how I understand delegates work, since I don’t 100% understand em, find a better way to explain it is a work for Sam :stuck_out_tongue:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms