You are on right track with the AddDynamic(…). However, parameters of the function passed in matter, thus getting it to work would require OnPickup() to be something like:
Using OnComponentBeginOverlap:
// In constructor
PickupMesh->OnComponentBeginOverlap.AddDynamic(this, &APickup::OnPickup);
// The method itself (Note the parameters)
void APickup::OnPickup(AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("PICKED UP!")));
}
Wait, are you using OnActorBeginOverlap or OnComponentBeginOverlap? If you use OnActorBeginOverlap then the function you add to this event should have signature
void function(class AActor* OtherActor)
but if you use OnComponentBeginOverlap then it’ll be
As you can see, there’s only one function called OnPickup() that when it’s called prints out a string.
Ok, but WHY does the string get printed out when I overlap the Health EVEN THOUGH there’s no link between the OnActorBeginOverlap event and the OnPickup()
Do you understand? OnPickup() is a simple function that isn’t supposed to be called from anywhere! Instead, it gets called when I overlap the actor in the world… why? O.o
Also, in the blueprint of that class, I detached any link from the event.
Ummm… Seems weird… Have you tried re-creating the blueprint? I’ve had issues with blueprints not updating their event delegate lists properly when events were added from C++ code. Like for some reason the function was still being bound to the event even though I had removed the line from constructor. Only way I could resolve those issues was to delete the derived blueprint and re-create it.
Ok I re-created the blueprints and its behavior is as I expected to be. Anyway, I added OnActorBeginOverlap.AddDynamic(this, &AHealth::OnPickup); in the constructor and
I’m not 100% sure, but I think that OnActorBeginOverlap is called when something overlaps the whole actor (any of its components), while OnComponentBeginOverlap is called only when something overlaps the specific component.
What comes to casting, AFirstPersonCharacter* OtherCharacter = Cast(OtherActor); should be sufficient. However, note that if OtherActor is not AFirstPersonCharacter, then cast returns NULL so you’ll have to take that in account.
EDIT: I forgot to specify the class for the cast, this should now be correct.