For example this will save an UObject into a valid UPackage asset:
UObject* UMGC_ScriptFactory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn)
{
check(Class->IsChildOf(UMagicNodeScript::StaticClass()));
return NewObject<UMagicNodeScript>(InParent,Class,Name,Flags,Context);
}
And this will save a new generated Blueprint class based on the same class:
UObject* UMGC_ScriptFactory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn)
{
check(Class->IsChildOf(UMagicNodeScript::StaticClass()));
return FKismetEditorUtilities::CreateBlueprint(Class,InParent,Name,BPTYPE_Normal,UBlueprint::StaticClass(),UBlueprintGeneratedClass::StaticClass(),TEXT("AssetTypeActions"));
}
This also creates a valid package and saves an UObject within:
void FMagicNodeEditor::CreateNewScriptAsset()
{
IAssetTools &AssetTools = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools").Get();
FString PackageName = FString(TEXT("/Game/"));
FString AssetName = FString(TEXT("NewMagicNode"));
AssetTools.CreateUniqueAssetName(PackageName, AssetName, PackageName, AssetName);
**UPackage* Package = CreatePackage(nullptr,*PackageName);**
**UPackage* OuterPack = Package->GetOutermost();**
auto ScriptFactory = NewObject<**UMGC_ScriptFactory**>();
UObject* NewScript = ScriptFactory->FactoryCreateNew(UMagicNodeScript::StaticClass(), OuterPack, *AssetName, RF_Standalone|RF_Public, nullptr, GWarn);
FAssetRegistryModule::AssetCreated(NewScript);
NewScript->MarkPackageDirty();
NewScript->PostEditChange();
NewScript->AddToRoot();
Package->SetDirtyFlag(true);
}