Can Anyone explain The Lines of C++ code Here?

Hey Guys! I’m just getting started with unreal engine4 and also C++ programming. I was following a tutorial provided in the documentation. I came across a few lines of code that I didn’t understood, so I was hoping that you guys can help me out here.

Here is the tutorial

Please Explain these two lines :
Sphere1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlap);
Sphere1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchCodeOnly::OnOverlap);

The first line is also giving me an error on XCode while building

Error : no matching member function for call to ‘__Internal_AddDynamic’

and What is the use of that int32 OtherBodyIndex in the OnOverlap function.

Please Help. Thanks

Those are delegate bindings, it’s even which can be detected outside the class and you bind function in to it, so it will be called when event will be triggered. It’s multicast delegate so you can bind as many functions as you like and they all will be called on event.

“Other” in UE terminology means object that triggered the event, so thats will give you body index of object that triggered the event.

Not sure what causing a error, it looks ok to me. OnOverlap should be declered the same way as example, like this:

void ALightSwitchCodeOnly::OnOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {}

Ofcorse ALightSwitchCodeOnly replace with class name you use

As the documentation says:

Designate the OnOverlap function as a delegate called when an Actor overlaps or leaves the SphereComponent.

You can search around for detailed explanations of delegates in programming, but very simply put, it allows you store some reference to a function in a variable, and you can use that variable to call the function.

In that line, “&ALightSwitchCodeOnly::OnOverlap” is referring to the function that must be called (address of (&) the OnOverlap function inside the class called ALightSwitchCodeOnly). Then, by Unreal magic, OnOverlap is added as a delegate to Sphere1->OnComponentBeginOverlap, which means it will be called whenever OnComponentBeginOverlap is called on the Sphere1.

As for OtherBodyIndex, I am not sure what that does.

I got the explanation…Thank You for that. But How can I solve the error. And it occurs for only the OnBeginOverlap Event.

Maybe try to rebuild your code

The error indicates that the compiler couldn’t find a function with the required signature. Have you tried defining ALightSwitchCodeOnly::OnOverlap as

UFUNCTION()
void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

?

Also, make sure that it is decorated with “UFUNCTION()”.