Using the following code I want to reset an object’s properties to defaults (CDO).
It appears the value on the memory address of the CDO is copied correctly to the target, but the editor window does not update. Properties on the editor panel show the old values and the yellow “reset to default” icons are still displayed. Running FKismetEditorUtilities::CompileBlueprint on the parent object does not fix this. Hitting the compile button manually does. What am I missing?
const UObject* DefaultObject = InTargetWidget->StaticClass()->GetDefaultObject();
for (TFieldIterator<FProperty> PropertyIterator(InTargetWidget->StaticClass()); PropertyIterator; ++PropertyIterator) {
FProperty* PropertyX = *PropertyIterator;
if (!PropertyX) {
continue;
}
#if WITH_EDITOR
if (!CanManageProperty(PropertyX)) {
continue;
}
#endif // WITH_EDITOR
void* CurrentValueAddress = PropertyX->ContainerPtrToValuePtr<void>(InTargetWidget);
const void* DefaultValueAddress = PropertyX->ContainerPtrToValuePtr<void>(DefaultObject);
#if WITH_EDITOR
InTargetWidget->PreEditChange(PropertyX);
#endif // WITH_EDITOR
PropertyX->CopyCompleteValue(CurrentValueAddress, DefaultValueAddress);
#if WITH_EDITOR
FPropertyChangedEvent PropertyEvent(PropertyX);
InTargetWidget->PostEditChangeProperty(PropertyEvent);
#else // Not WITH_EDITOR
// Still need to synchronize the widget, which is otherwise done during PostEditChangeProperty.
TSharedPtr<SWidget> SafeWidget = InTargetWidget->GetCachedWidget();
if (SafeWidget.IsValid()) {
InTargetWidget->SynchronizeProperties();
}
#endif // WITH_EDITOR
}