In my character class I want an UPROPERTY, which I can access from a blueprinted version and into which I can insert any class that implements an interface “Weapon” I defined. Now I put this in my character header (because that’s what the compiler recommended to me)
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Weapon)
TScriptInterface<class IWeapon> Weapon;
Then I compiled the code and blueprinted the class only to find out that I’m unable to put anything into that “Weapon” UPROPERTY. I created a class “Pistol”, which implements the Interface and neither that class nor a BP of it can be inserted into the “Weapon” slot.
Weapon.h
#pragma once
#include "Weapon.generated.h"
/**
*
*/
UINTERFACE(MinimalAPI, Blueprintable)
class UWeapon : public UInterface
{
GENERATED_BODY()
};
class IWeapon
{
GENERATED_BODY()
public:
virtual void Fire(FVector from);
};
Weapon.cpp
#include "Weapon.h"
void IWeapon::Fire(FVector from)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("SCHUSS"));
}
Pistol.h
#pragma once
#include "CoreMinimal.h"
#include "Weapon.h"
#include "UObject/NoExportTypes.h"
#include "Pistol.generated.h"
/**
*
*/
UCLASS(Blueprintable)
class UPistol : public UActorComponent, public IWeapon
{
GENERATED_BODY()
public:
virtual void Fire(FVector from) override;
UPROPERTY(EditAnywhere, Category="Shooting")
TSubclassOf<class ABullet> BulletBP;
};
Pistol.cpp
#include "Pistol.h"
#include "Bullet.h"
void UPistol::Fire(FVector from)
{
FTransform SpawnTransform(from);
GetWorld()->SpawnActor<ABullet>(BulletBP);
}