Search code examples
c++unreal-engine4

How to LaunchCharacter from another class


I've created a LaunchPad actor C++ class which is compiled of a Cube static mesh component and a UBox component. As of now, once the character overlaps the hit box it triggers whatever is inside the ALaunchPad::OnBoxBeginOverlap. I am current trying to tell that when the character overlaps the UBox launch them in the air similair to the Blueprints 'LaunchCharacter' node.

LaunchPad.cpp

void ALaunchPad::OnBoxBeginOverlap(UPrimitiveComponent*  OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 
{
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Character Launched!"));
    Asgd240_1115350_core5Character::LaunchCharacter(FVector(0, 0, velocity), false, true);

}

LaunchPad.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LaunchPad.generated.h"


UCLASS()
class SGD240_1115350_CORE5_API ALaunchPad : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    ALaunchPad();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    void OnBoxBeginOverlap(UPrimitiveComponent * HitComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit);

    UPROPERTY(VisibleAnywhere)
    class UBoxComponent* Collide;

    UFUNCTION()
    void OnBoxBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

    UPROPERTY(EditAnywhere)
        float velocity;

    UFUNCTION()
        void LaunchCharacter();

};

At the moment I am recieving a 'non static member reference must be relative to a specific object and it won't compile. Any ideas?


Solution

  • Cast OtherActor to an ACharacter then call its LaunchCharacter method:

    void ALaunchPad::OnBoxBeginOverlap(UPrimitiveComponent*  OverlappedComp, AActor* OtherActor, 
            UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, 
            const FHitResult& SweepResult) 
    {
        GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Character Launched!"));
    
        ACharacter* OtherCharacter = Cast<ACharacter>(OtherActor);
        if (OtherCharacter != nullptr)
        {
            OtherCharacter->LaunchCharacter(FVector(0, 0, velocity), false, true);
        }
    
    }