Hey guys.
I’m trying to solve a problem of serialization in my 'lil project: I need to be able to take any UStruct from a class that would inherit my Interface and dump it into JSON object.
So far I’ve come up with a solution that looks like this:
Interface:
UINTERFACE(Blueprintable)
class NBS_API USavable : public UInterface
{
GENERATED_UINTERFACE_BODY()
};
class ISavable
{
GENERATED_IINTERFACE_BODY()
public:
....
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Saving")
UStruct* GetSaveData();
....
};
Example of class using it:
USTRUCT()
struct FCompValue
{
GENERATED_BODY()
int32 foo;
bool bar;
FHealthComponentValues();
};
UCLASS( ClassGroup=(Custom), Blueprintable, meta=(BlueprintSpawnableComponent) )
class NBS_API UHealthComponent : public UActorComponent, public ISavable
{
GENERATED_BODY()
FCompValue comValue;
... code ...
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Saving")
UStruct* GetSaveData();
virtual UStruct* GetSaveData_Implementation() override { return (UStruct*)&compValue; }
... more code ...
};
it this point when I collect those UStructs with my manager, they all come out to be garbage, which is understandable because I’m down-casting them to UStruct.
I figured out to use templates for this problem, so I would want to make something like:
template<typename Data>
class ISavable
{
GENERATED_IINTERFACE_BODY()
public:
....
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Saving")
Data* GetSaveData();
....
};
such that I can get those structs directly without down-casting them into just UStruct.
I’ve been looking around forums and Wiki’s to find a proper solution to templated interfaces in Unreal, but I could not… So here I’m asking for your advice.
Thanks!