# Knowledge Base: How To: Create New Assets in C++

**URL:** <https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [December 8, 2021, 10:14pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944 "2021-12-08T22:14:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RudyTriplett](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rudytriplett/32/1197584_2.png) [@RudyTriplett](https://forums.unrealengine.com/u/RudyTriplett)\
**Post date:** [December 8, 2021, 10:14pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/1 "2021-12-08T22:14:54Z")

</div>

Article written by Cody A.  
When writing editor scripts or plugins, it may be useful to be able to programmatically create a new asset that appears in the content browser. There are a few helper functions available to he…  
  
[https://dev.epicgames.com/community/learning/knowledge-base/wzdm/unreal-engine-how-to-create-new-assets-in-c](https://dev.epicgames.com/community/learning/knowledge-base/wzdm/unreal-engine-how-to-create-new-assets-in-c)

---

<div class="post-metadata">

**Author:** ![Love\_Less](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Love\_Less](https://forums.unrealengine.com/u/Love_Less)\
**Post date:** [February 10, 2023, 8:47pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/2 "2023-02-10T20:47:34Z")

</div>

Hi, can somebody provide a small sample how this works with DataAssets? Can I use UDataAssetFactory here? This snippet is a good starting point, but a more complete example would be much appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![A.RADIX](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@A.RADIX](https://forums.unrealengine.com/u/A.RADIX)\
**Post date:** [August 18, 2023, 1:16pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/3 "2023-08-18T13:16:16Z")

</div>

If you wish to utilize DataAssets in this context, you can achieve this by making a minor adjustment to the default code:

Instead of the original code:

```auto
UBlueprintFactory* MyFactory = NewObject<UBlueprintFactory>(UBlueprintFactory::StaticClass()); // This line is optional; a default factory will be used if omitted
UObject* NewObject = AssetToolsModule.Get().CreateAsset(Name, PackagePath, UBlueprint::StaticClass(), MyFactory);

```

Use the following code:

```auto
UDataAssetFactory* MyFactory = NewObject<UDataAssetFactory>(UDataAssetFactory::StaticClass());
MyFactory->DataAssetClass = UPrimaryAssetLabel::StaticClass();
UObject* NewObject = AssetToolsModule.Get().CreateAsset(Name, PackagePath, UDataAsset::StaticClass(), MyFactory);

```

In my case, I employed a `UPrimaryAssetLabel` type for my `UDataAsset`; however, you are free to choose the appropriate type for your specific needs.

Inside the constructor of `UDataAssetFactory` in the Unreal Engine source code, we can observe that it employs:  
`SupportedClass = UDataAsset::StaticClass();`

Hence, when we employ the CreateAsset function with this Factory, it’s important to replace “UBlueprint” with “UDataAsset”.

The line `MyFactory->DataAssetClass = UPrimaryAssetLabel::StaticClass();` is crucial to avoid crashes. We can deduce its necessity by inspecting the `UAssetToolsImpl::CreateAsset` function, which invokes `UDataAssetFactory::FactoryCreateNew`, utilizing the `TSubclassOf<UDataAsset> DataAssetClass` parameter.  
This parameter is also present in UDataAssetFactory, and UPrimaryAssetLabel inherits from UDataAsset.

---

<div class="post-metadata">

**Author:** ![Shen\_Yuju](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shen\_Yuju](https://forums.unrealengine.com/u/Shen_Yuju)\
**Post date:** [September 14, 2023, 3:16pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/4 "2023-09-14T15:16:38Z")

</div>

May I ask, what is “PackageName” for an asset?  
If I wanna create a BP inherit from a AActor, what “PackageName” should I set for it?

---

<div class="post-metadata">

**Author:** ![GarbaGod](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GarbaGod](https://forums.unrealengine.com/u/GarbaGod)\
**Post date:** [December 12, 2023, 4:12pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/5 "2023-12-12T16:12:43Z")

</div>

I’m Taiwanese, forgive about my english.  
Here’s the thing, i’ve encountered a problem that I want to programmatically generate assets, like inventory items. Yeah, I know the way of using AssetData, but some object is already set, so I still want to create a blueprint asset into content.

Here is it.  
Before defining your own generation code, you have to define your object factory first.

```auto
UCLASS( hidecategories = Object, collapsecategories)
class LYRAGAME_API UBPFactory_MyObject: public UBlueprintFactory
{
	GENERATED_BODY()

	UBPFactory_MyObject();

	virtual UObject* FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn, FName CallingContext) override;
};

UBPFactory_MyObject::UBPFactory_MyObject()
{
	bCreateNew = true;
	bEditAfterNew = true;
	SupportedClass = UBlueprint::StaticClass();
	ParentClass = ULyraInventoryItemDefinition::StaticClass();
	BlueprintType = BPTYPE_Normal;
}

UObject* UBPFactory_MyObject::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags, UObject* Context, FFeedbackContext* Warn, FName CallingContext)
{
	return FKismetEditorUtilities::CreateBlueprint(ParentClass, InParent, Name, BlueprintType, UBlueprint::StaticClass(), UBlueprintGeneratedClass::StaticClass(), CallingContext);
}

```

ObjectFactory is the main method of creating your object.

After creating BlueprintFactory for your object, back to CreatePackage section.

```auto
// Create object and package
UPackage* package = CreatePackage(*PackageName);
UBPFactory_MyObject* MyFactory = NewObject<UBPFactory_MyObject>(UBPFactory_MyObject::StaticClass()); // Use tour custom object factory

UObject* NewObject = AssetToolsModule.Get().CreateAsset(Name, OutPackagePath, UBlueprint::StaticClass(), MyFactory);

FSavePackageArgs SaveArgs = FSavePackageArgs();
SaveArgs.SaveFlags = RF_Public | RF_Standalone;

UPackage::Save(package, NewObject, *FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension()), SaveArgs);

// Inform asset registry
AssetRegistry.AssetCreated(NewObject);

// Tell content browser to show the newly created asset
TArray<UObject*> Objects;
Objects.Add(NewObject);

```

That’s much all for generating customed blueprint assets.

---

<div class="post-metadata">

**Author:** ![Ninatura](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ninatura](https://forums.unrealengine.com/u/Ninatura)\
**Post date:** [April 10, 2024, 8:57pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/6 "2024-04-10T20:57:05Z")

</div>

Hi! I’m having a problem where I can’t seem to use the StaticClass() of the UDataAssetFactory. It gives a linkin error. Do you know what this could be?

---

<div class="post-metadata">

**Author:** ![A.RADIX](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@A.RADIX](https://forums.unrealengine.com/u/A.RADIX)\
**Post date:** [April 16, 2024, 3:51pm UTC](https://forums.unrealengine.com/t/knowledge-base-how-to-create-new-assets-in-c/264944/7 "2024-04-16T15:51:53Z")

</div>

Hello, what are the error logs ?
