How to Edit An Instanced Property through Code

It’s known that we can use UPROPERTY specifier Instanced to mark a property as instanced. For example in the following code snippet, I created a class UInstancedPropertyBase with DefaultToInstanced UCLASS specifier and an actor class AMyActor containing an Instanced property of class UInstancedPropertyBase.

UCLASS(Abstract, DefaultToInstanced, EditInlineNew, Blueprintable, BlueprintType)
class COMPONENTCAMERASYSTEM_API UInstancedPropertyBase : public UObject
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere)
	int Int;

	UPROPERTY(EditAnywhere)
	double Double;
};

UCLASS(BlueprintType, Blueprintable)
class COMPONENTCAMERASYSTEM_API AMyActor : public AActor
{
	GENERATED_BODY()

public:
	UPROPERTY(Instanced, EditAnywhere)
	TArray<TObjectPtr<UInstancedPropertyBase>> InstancedProperties;
};

Then in the editor, I created two blueprint classes inheriting from UInstancedPropertyBase and one blueprint class inheriting from AMyActor.

Snipaste_2024-08-01_16-50-39

Open the MyActor_2 blueprint and I can choose which class of Instanced_1 and Instanced_2 to instantiate. Everything goes well right now.

Snipaste_2024-08-01_16-52-54

My question is, can I edit the instanced property in MyActor_2 through code, not just opening the blueprint and choose the class. What I want is:

  • Specify the class of the instanced property, i.e., Instanced_1 or Instanced_2.
  • Specify the internal values of the instanced class, i.e., the Int and Double values.
  • If it’s an array, I can add or remove one or more instanced classes.

I’m trying to access the FProperty of the CDO, but it seems infeasible because I’m trying to assign a runtime address to the instanced property of the CDO.

1 Like

Any ideas?

Okay, I found this function UEngine::CopyPropertiesForUnrelatedObjects(UObject* OldObject, UObject* NewObject, FCopyPropertiesForUnrelatedObjectsParams Params) serves my purpose quite well, except that it’s over-doing what I want: I want to just copy an instanced property to CDO but this function does it for the whole actor.

For a single instanced property, I found DuplicateObject can do most of the work. The problem with this function is that is cannot handle nested instanced properties.
For example, class A is instanced, and it has a B class member as an inner instanced property. Using DuplicateObject cannot copy B in a nested way.

Forget to mention: my final goal is to “copy” the instanced property from an instance to its CDO.