How to make a runtime transient derived class with a custom property value?

I think I’ve gotten pretty close – I think I’ve figured out how to make the runtime transient class, and how to grab a reference to the property itself. However, I haven’t figured out how to set the property.

This is what I have so far. It compiles and runs, but when the AnimInstance is actually created, the property value is still null, instead of the value I wanted to assign.

		UClass* templateClass = UAnimCopyOrRetargetInstance::StaticClass();
		FName genClassName = FName(FString::Printf(TEXT("gen_%s_from_%s"), *templateClass->GetName(), *skeleton->GetName()));
		UClass* genClass = NewObject<UClass>(GetTransientPackage(), genClassName, RF_Public | RF_Transient);
		genClass->ClassFlags |= CLASS_Transient;
		genClass->PurgeClass(false);

		genClass->PropertyLink = templateClass->PropertyLink;
		genClass->ClassAddReferencedObjects = templateClass->ClassAddReferencedObjects;
		genClass->SetSuperStruct(templateClass);
		genClass->StaticLink(true);
		genClass->AssembleReferenceTokenStream();

		FProperty* prop = genClass->FindPropertyByName(GET_MEMBER_NAME_CHECKED(UAnimCopyOrRetargetInstance, mCopySource));
		FObjectPtrProperty* objPtrProp = Cast<FObjectPtrProperty>(prop);
		HV_ASSERT(objPtrProp);
		objPtrProp->SetObjectPropertyValue_InContainer(genClass, rootSkmComp);

		genClass->Bind();
		objPtrProp->SetObjectPropertyValue_InContainer(genClass, rootSkmComp);
		objPtrProp->SetObjectPropertyValue_InContainer(genClass->GetDefaultObject(), rootSkmComp);

		subComponent->SetAnimInstanceClass(genClass);

Context: I made a native AnimInstance class that generates a CopyPoseFromMesh or RetargetPoseFromMesh, depending on what’s required.

Originally, I just set the AnimInstanceClass, then called RegisterComponent(), grabbed the AnimInstance instance, and set the value I wanted. However, that AnimInstance value isn’t being persisted from editor to PIE, so I suspect the AnimInstance is being recreated from its class at runtime, which seems to imply that I need to make a new derived class that contains the value I want as part of its default properties.

For reference, I’ve also tried assigning the value directly to the CDO instance, but that doesn’t seem to persist the value either

I ended up addressing this issue by just modifying my AnimClass’s constructor to manually copy the properties I want from my class’s default object.

UAnimCopyOrRetargetInstance::UAnimCopyOrRetargetInstance() {
	/* other not-relevant logic */

	if (!IsTemplate()) {
		UAnimCopyOrRetargetInstance* CDO = Cast<UAnimCopyOrRetargetInstance>(GetClass()->GetDefaultObject());
		mCopyFromComponentName = CDO->mCopyFromComponentName;
	}
}

For reference, this is the logic I ended up using for dynamically generating a runtime transient derived class:

// static member function
UClass* UAnimCopyOrRetargetInstance::MakeDerivedClass(FString copyFromComponentName) {
	UClass* templateClass = StaticClass();

	FName genClassName = FName(FString::Printf(TEXT("gen_%s_from_%s"), *templateClass->GetName(), *copyFromComponentName));
	UClass* genClass = Cast<UClass>(StaticFindObjectFast(nullptr, GetTransientPackage(), genClassName, true));
	if (genClass) {
		return genClass;
	}

	genClass = NewObject<UClass>(GetTransientPackage(), genClassName, RF_Public | RF_Transient);
	genClass->PurgeClass(false);
	genClass->ClassFlags |= CLASS_Transient;

	genClass->PropertyLink = templateClass->PropertyLink;
	genClass->ClassAddReferencedObjects = templateClass->ClassAddReferencedObjects;
	genClass->SetSuperStruct(templateClass);
	genClass->StaticLink(true);
	genClass->AssembleReferenceTokenStream();
	genClass->Bind();

	UAnimCopyOrRetargetInstance* CDO = Cast<UAnimCopyOrRetargetInstance>(genClass->GetDefaultObject());
	CDO->mCopyFromComponentName = copyFromComponentName;

	return genClass;
}