Overview of my Project now:
I have 1 Blueprint class, 2 C++ classes, and an interface now:
BP_MyCharacter
AMoneyBag
AMedKit
ILoot
ILoot
:
UINTERFACE(BlueprintType, Blueprintable, MinimalAPI)
class ULoot : public UInterface
{
GENERATED_BODY()
};
class DETECTIVEINTERACTION_API ILoot
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Interaction", meta=(HideSelfPin))
void PickUp();
};
AMoneyBag
:
UCLASS(Blueprintable, BlueprintType)
class AMoneyBag : public AActor, public ILoot
{
GENERATED_BODY()
public:
AMoneyBag();
void PickUp_Implementation() override;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPickUpMoneyBagSignature, in32, PickedMoney);
UPROPERTY(BlueprintAssignable, BlueprintCallable)
FOnPickUpMoneyBagSignature OnPickUp;
protected:
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Money", meta=(AllowPrivateAccess))
int32 Money;
};
void AMoneyBag::PickUp_Implementation()
{
OnPickUp.Broadcast(Money);
Destroy();
}
AMedicalKit
:
UCLASS(Blueprintable, BlueprintType)
class AMedicalKit : public AActor, public ILoot
{
GENERATED_BODY()
public:
AMedicalKit();
void PickUp_Implementation() override;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnPickUpMedKitSignature, int32, PickedHealth);
UPROPERTY(BlueprintAssignable, BlueprintCallable)
FOnPickUpMedKitSignature OnPickUp;
protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Health", meta=(AllowPrivateAccess))
int32 Health;
};
void AMedicalKit::PickUp_Implementation()
{
OnPickUp.Broadcast(Health);
Destroy();
}
In BeginPlay
of BP_MyCharacter
I iterate over all ILoot
implementing objects and, with regular casts to AMoneyBag
and AMedicalKit
, bind to the delegates with events PickUpMoney
and PickUpMedKit
. As you can tell, this doesn’t scale very well:
The only shared logic between AMoneyBag
and AMedicalKit
is the “picking up” functionality consisting of an implementable function and specialized delegates. The types and other functions are different.
The question:
I wonder if there is any possible way to make it more generic. The part that I don’t like specifically is where I bind to all ILoot
actors in the scene and then cast to specific classes. I want my classes to stay as decoupled as they can be, that’s why I’m trying to use interfaces and delegates as much as possible.
I am probably completely wrong
My guess is to use an abstract class for AMoneyBag
and AMedicalKit
and future “Loot” classes, though it seems like not the best solution. Seeking for an architectural advice.