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 ?
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?
0xd0d9e
(0xd0d9e)
July 2, 2024, 6:28pm
6
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
Yazhuo
(Yazhuo)
September 1, 2024, 8:19am
7
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?
Yazhuo
(Yazhuo)
January 7, 2025, 2:01am
9
No idea.
Someone tell me that you can write reflection infomation in other place. but not the detail.
I want to giveup,cause I just need the templated stuct for int and float. I create them in traditional method.
But it still is a interesting study.