Templated USTRUCT?

Is there any way to use templates/generics on USTRUCT objects (or even UCLASS)?

For example, something like the following:

USTRUCT(Blueprintable, Category = "Scripting|Remote Character|Attributes")
template<typename Type>
struct FRemoteCharacterSubAttribute
{
	GENERATED_USTRUCT_BODY();
public:
	FRemoteCharacterSubAttribute() {
		Name = FString(L"Unassigned");
		BaseCost = 0.0f;
	}

	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	FString Name;
	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	float BaseCost;
	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	Type MinValue;
	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	Type Value;
	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	Type MaxValue;
};

… And then you would use it like:

USTRUCT(Blueprintable, Category = "Scripting|Remote Character|Attributes")
struct FRemoteCharacterAttributes
{
	GENERATED_USTRUCT_BODY();
public:
	FRemoteCharacterAttributes() {
		fMovement.Name = FString("Movement");
		fMovement.BaseCost = 10.0f;
		fMovement.MinValue = 50.0f;
		fMovement.MaxValue = 500.0f;
		fMovement.Value = fMovement.MinValue;
	}

	UPROPERTY(BlueprintReadWrite, Category = "Scripting|Remote Character|Attributes")
	FRemoteCharacterSubAttribute<float> fMovement;
};

Any way to make this happen?

Hi Michael,

Template USTRUCTs, UCLASSES, UFUNCTIONs. etc. are not supported in any form, and are unlikely to be.

Steve

Is there any change on the subject of templates ?
Or any way to generate ustructs ?

Any news on this front?

template<typename T>
struct FTemplateStruct: public TSharedFromThis<FTemplateStruct> {

	TSubclassOf<T> Class;

	T* Object;

};

this is also not working, then the question, how to make sucha struct not to be cleared by garbage collector?

There is an error in your code from the C++ point of view.
You have incorrectly specified the type in TSharedFromThis.

This should work correctly

template<typename T>
struct FTemplateStruct: public TSharedFromThis<FTemplateStruct<T>> {

	TSubclassOf<T> Class;

	T* Object;
};

The topic starter has a different problem that still hasn’t been solved. Apparently, UBT does not support templates, so it cannot generate code related to reflection support for them.

P.S. I apologise for the necro post.

1 Like

So,if we can rigister reflection after creating template struct?
Reading sorce code “Range.h”,and you will find that all of Range are created by template,but they can use Property System. I dont know how they do it,and where they rigister reflection.
Is any idea?