【UE4】C++の静的関数をBPで使えるようにする

今回はちょっと短めです💦

こちらの記事を参考に静的関数をBPで使えるようにする方法を試しました

www.gamecorder.net

ヘッダーはこんな感じ

UCLASS()
class PONGCPPPROJECT_API UBallStaticFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
        UFUNCTION(BlueprintCallable, Category = "Ball", BlueprintPure)
        static FVector BallImpluse(float Yaw, float Speed);

        UFUNCTION(BlueprintCallable, Category = "Ball", BlueprintPure)
        static FVector RefrectedVector(float Yaw, FVector Normal);

        UFUNCTION(BlueprintCallable, Category = "Ball", BlueprintPure)
        static float AddSpeed(FVector velocity, float add, float limit);
};

ソースコードはこんな感じ

FVector UBallStaticFunctionLibrary::BallImpluse(float Yaw, float Speed) {
    FRotator rot(0,Yaw,0);
    FVector forwordVec = rot.RotateVector(FVector(1,0,0));  //X軸をYawの分だけ回転させる
    return forwordVec * Speed;
}
FVector UBallStaticFunctionLibrary::RefrectedVector(float Yaw, FVector Normal){
    return FRotator(0, Yaw, 0).RotateVector(FVector(1, 0, 0)).MirrorByVector(Normal);
}
float UBallStaticFunctionLibrary::AddSpeed(FVector velocity, float add, float limit) {
    float x = abs(velocity.X);
    float y = abs(velocity.Y);

    if (x <= limit && y <= limit) {
        return add;
    }

    return 0;
}

こちらの例はPongで使うために作った跳ね返るボールの静的関数のライブラリです。

ポイントはこんな感じでしょうか?

  • UFUNCTIONの中で設定していきます
  • BlueprintCallableとすると関数として使うことができます。
  • Categoryで指定した名前で分類されます
  • BlueprintPureと指定するとPure関数になります。(▷が無くなる)

BPだけだと冗長になりそうな部分をC++でまとめるなどは出来そうですね。

もっとも、BP自体を分割は出来るので、C++にするという意味があるかどうかは微妙ですが💦

今回はこんな感じで、また会いましょう😄