C++ Interfaces Accessible In blueprints

From Unreal Wiki
Revision as of 01:47, 17 February 2025 by Unreal583 (talk | contribs)
Jump to navigation Jump to search

To create a c++ interface, add a new c++ class and under common classes select Unreal Interface



This generates two classes U<ClassName> and I<ClassName>
You put your virtual functions in the I<ClassName>
In the example below we added ShootWeapon to the appropriate class

<syntaxhighlight lang="cpp">

// This class does not need to be modified. UINTERFACE(MinimalAPI) class UInoWeaponInterface : public UInterface { GENERATED_BODY() };

/**

* 
*/

class ININO0_API IInoWeaponInterface { GENERATED_BODY()

// Add interface functions to this class. This is the class that will be inherited to implement this interface. public: virtual bool ShootWeapon(); };

</syntaxhighlight>

Implementing the Interface

All you need to do is include the header file for your interface in the header file where you want to use the interface

and Inherit from the interface

<syntaxhighlight lang="cpp">

  1. include "CoreMinimal.h"
  2. include "GameFramework/Actor.h"
  3. include "CI_Item.h"
  4. include "In0_Item.generated.h"

UCLASS() class ININO0_API AIn0_Item : public AActor , public ICI_Item { GENERATED_BODY() public: // Sets default values for this actor's properties AIn0_Item(); protected: // Called when the game starts or when spawned virtual void BeginPlay() override; public: // Called every frame virtual void Tick(float DeltaTime) override; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Details") USkeletalMeshComponent *SkeletalMesh; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item Details") }; </syntaxhighlight>

In c++ you must implement all virtual functions if you include them, if you don't you will get alot of errors