How to cast in cpp

Ok so i want to create a moving platform that when the player gets on top of it it starts moving up and when the player leaves the platform is stops moving!
!

This is the blueprint that i made and i want to make the same thing in cpp

Here is the cpp code. When the player overlaps the trigger it starts moving up but when the player jump off the platform the platform doesn’t stop

I’ve had issues using the Shape Component’s Overlap Event signatures in C++.

My solution was to use the actor Begin and End overlap events. Try this:

// MovingPlatform.h

virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
virtual void NotifyActorEndOverlap(AActor* OtherActor) override;

// MovingPlatform.cpp

void AMovingPlatform::NotifyActorBeginOverlap(AActor* OtherActor)
{
    Super::NotifyActorBeginOverlap(OtherActor);
    if (Cast<AFP_FirstPersonCharacter(OtherActor))
    {
        bMoveUp = true;
    }
}

void AMovingPlatform::NotifyActorEndOverlap(AActor* OtherActor)
{
    Super::NotifyActorEndOverlap(OtherActor);
    if (Cast<AFP_FirstPersonCharacter(OtherActor))
    {
        bMoveUp = false;
    }
}

These functions will be called when ANY component was overlapped in your actor.

I had a similar situation when I was making Capture Points and the Shape Component’s Overlap Event Signatures did not execute, so I solved it by using these functions.

Let me know if it helps!

~ Dennis “MazyModz” Andersson

Yes thank you that works! But i have some questions

  1. Why we have to define that its virtual void and its override?
  2. Why do we have to type Super::NotifyActorBeginOverlap?
  3. Every component on the AMovingPlatform class that has a collision volume and it gets overlapped it is gonna trigger that event?

A virtual function mean that it can be overriden in a derived / child class.

In this case NotifyActorBeginOverlap and NotifyActorEndOverlap is defined in AActor. So in order to change the logic of those functions in your child class you can override it providing it’s marked virtual.

Super::NotifyActorBeginOverlap means that we are executing all functionality that was defined in the parent AActor implementation. This is to prevent ‘loosing’ anything that happens in the parent AActor implementation.

And finally yes, if anything overlaps any component in your platform actor that is set to overlap, those functions will be called.

Okay, thank you man!

Unless I am wrong, in this instance, calling Super:: doesn’t do anything else than call a possible derived blueprint event of OnBeginOverlap? Seems that in most cases you’d prefer not to call it, because that event fires on its own as well, and calling the Super:: on C++ side will mean that the event will fire twice.