How can I set Blueprint custom property from C++

I created subclass of my Blueprint class from c++. And I want to setup properties of the Blueprint subclass like following codes. This codes finished without warning or errors, but value is not set in Editor. How can I do this?

I confirmed I can set property to this subclass in Blueprint Editor manually. But I want do this with C++ plugin because there are so many assets.

void SubBPFactory::SetupSubBP(FString BPName, bool SomePropertyValue)
	UBlueprint* TargetParentBP = (UBlueprint*)StaticLoadObject(UBlueprint::StaticClass(), NULL, TEXT("/Game/BluePrints/ParentBP"), NULL, LOAD_NoWarn, NULL);
	UClass* TargetParentClass = TargetParentBP->GeneratedClass;

	UBlueprintFactory* BlueprintFactory = NewObject<UBlueprintFactory>();
	BlueprintFactory->ParentClass = TargetParentBP->GeneratedClass;

	FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools");
	UObject* newObject = AssetToolsModule.Get().CreateAsset(BPName, TEXT("/Game/BluePrints/SubClasses"), TargetParentBP->GetClass(), BlueprintFactory, FName("CustomNewAsset"));
	UBlueprint* NewAsset = Cast<UBlueprint>(newObject);

	UBoolProperty * BoolProp = FindField<UBoolProperty>(TargetParentClass, TEXT("SomeProperty"));
	if (BoolProp)
		void* Object = BoolProp->ContainerPtrToValuePtr<void>((void*)newObject);
		BoolProp->SetPropertyValue(Object, SomePropertyValue);

You’re not setting the memory in the place you want. newObject is just the blueprint asset itself. You need to set the property on the CDO of the blueprint’s class. Try this:

auto CDO = NewAsset->GeneratedClass->GetDefaultObject();
BoolProp->SetPropertyValue_InContainer(Object, SomePropertyValue, 0);

Going from memory, but I’ve done something similar before, I think that’s right. Also make sure to either mark the asset as modified, or save the package.

1 Like

Check this:

1 Like