Redirecting Struct Members to Class

Hey,

I have a struct as a member of a class and I’d like to move the members of the struct into the class.
Here’s the rough layout:

USTRUCT()
struct FWeaponData
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditDefaultsOnly, Category = Config)
	int32 Ammo;
};

UCLASS(Abstract, Blueprintable)
class SHOOTERUE4_API AWeapon : public AActor
{
	GENERATED_UCLASS_BODY()
public:
	UPROPERTY(EditDefaultsOnly, Category = Config)
    FWeaponData WeaponConfig;
	UPROPERTY(EditDefaultsOnly, Category = Config)
	int32 Ammo;
};

I’ve tried using K2FieldRedirectors within DefaultEngine.ini like this:

[/Script/Engine.Engine]
+K2FieldRedirects=(OldFieldName="Weapon.WeaponConfig.Ammo",NewFieldName="Weapon.Ammo")

But it doesn’t seem to work. This approach does work for moving item between classes or simply renaming them, but it doesn’t work here. Any ideas where I might be going wrong?

You’ll need to use deprecation to accomplish this.

WeaponConfig should be renamed WeaponConfig_DEPRECATED and all the elements in the UPROPERTY macro should be removed (it must still be a property though).

You’ll then need to override PostLoad and transfer the data out of the old property and in to the new one. You’ll need some kind of sentinel to identify when that should occur. If the default value will remain the same then that can work well since the first time the data is loaded the old property will have the saved value but after subsequent saves and load it will now be the default so you know not to move it. Alternatively you could have an additional Boolean property to indicate the migration has occurred or to use your projects object version to identify when the value needs moving.

If you search the code base for _DEPRECATED you’ll see a number of examples of this being done and can pattern off of it.

Thanks Marc, I’ll try this out asap. I’m curious though, why don’t redirectors work?

K2FieldRedirects are specific to the properties within blueprint nodes in a graph.

The closest thing to what you wanted is TaggedPropertyRedirects, however, they only work within the same scope. There is no concept of hierarchy so you can’t use them to move data around. They exist primarily to allow renaming of properties without having to deprecate one property and add a new one.

Interesting. I’ve found a few good examples of _DEPRECATED too, so I’m pretty sure I should be able to fix my issue.