Help us understand the problem. What is going on with this article?

【UE4】C++でInterfaceを使う方法

More than 3 years have passed since last update.

C++インターフェース

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

#pragma once

#include "SampleInterface.generated.h"


UINTERFACE(Blueprintable)
class USampleInterface : public UInterface
{
    GENERATED_UINTERFACE_BODY()
};


/**
 * サンプルインターフェース
 */
class ISampleInterface {

    GENERATED_IINTERFACE_BODY()

public: // 非const版

    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    void Sample();
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    void SampleArg(int32 Value);
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    int32 SampleResult();
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    int32 SampleArgResult(int32 Value);

public: // const版
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    void SampleConst() const;
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    void SampleArgConst(int32 Value) const;
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    int32 SampleConstResult() const;
    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Sample")
    int32 SampleArgConstResult(int32 Value) const;

};

ブループリント側に実装

クラスの詳細->詳細パネル->インターフェースの実装を選んでください。
image

どこに実装するか

戻り値\const 非const版 const版
戻り値無し イベントグラフに実装 関数側に実装
戻り値有り 関数側に実装 関数側に実装

実装方法

  • イベントグラフ

    イベントを追加->C++で書いたイベント名を選択してください。
    image

  • 関数側
    自動的にマイブループリントの欄にインターフェースが追加されるのでそちらに実装を書いてください
    image

C++での使い方

  • インターフェースを実行
UObject* Object;

if (Object->GetClass()->ImplementsInterface(USampleCppInterface::StaticClass()))
{
    ISampleCppInterface::Execute_Sample(Object);
}
  • インターフェースを実装しているActorすべてに関数を実行
TArray<AActor*> OutActors;

// インターフェース取得
UGameplayStatics::GetAllActorsWithInterface(this->GetWorld(), USampleCppInterface::StaticClass(), OutActors);

// インターフェースを持っている対象すべての実行を行う
for (AActor* Actor : OutActors)
{
    ISampleCppInterface::Execute_Sample(Actor);
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away