I plan to replace a series of UProperty variables (a series of FTransform) with a TArray of them.
Is there a way I can migrate these UProperties automatically (or semi-automatically)?
Ideally I’d like to make a post-package-load thing to read the properties, see if they’re not in the TArray, and then add them before zeroing out the affected UProperty, and calling save.
Since this thread actually comes up on Google for queries related to this, I’m answering this even though it’s 5 years old.
You can do this by:
- Rename the old properties with the _DEPRECATED suffix. Remove any “Edit” or “BlueprintRead/Write” specifiers from their UPROPERTY tag (but keep the tag itself). This magic suffix allows Unreal to deserialize the old values into the properties, but it will not save them out if the asset is resaved. The suffix has the added benefit of alerting you to uses you need to update (since they will be compiler errors).
- Override PostLoad. In it, if the old properties have values and the new one doesn’t, do whatever migration you need to do to move them over.
If you like, you can delete the deprecated properties and PostLoad code after resaving all affected assets, or keep it around if you can’t guarantee there aren’t more unsaved assets somewhere (shelved in Perforce or whatever).
UCLASS()
class UMyClass : public UObject
{
GENERATED_BODY()
public:
UPROPERTY()
int OldValue_DEPRECATED;
UPROPERTY(EditAnywhere)
TArray<int> NewValue
virtual void PostLoad() override
{
Super::PostLoad();
if (OldValue_DEPRECATED != 0 && NewValue.Num() == 0)
{
NewValue.Add(OldValue_DEPRECATED)
OldValue_DEPRECATED = 0; // mostly unnecessary since this property will not be reserialized
}
}
}