How do i check assets for references?

Hi,
i want to check the assets for references but all referencers/dependencies are always zero. Can anyone help, please?
Here is my code:

void FCleanUp_PluginModule::SearchForAssets()
{
UE_LOG(LogTemp, Warning, TEXT(“Searching For Assets”));

//path to search
const FString DirectoryFolder = FPaths::ProjectContentDir();
//set FileExtension to NULL to get all files with any extension
const FString FileExtension = "";
//array to store found files
TArray<FString> FoundFiles;
//find files in contentbrowser
FPlatformFileManager::Get().GetPlatformFile().FindFiles(FoundFiles, *DirectoryFolder, *FileExtension);

FAssetRegistryModule& AssetRegistryModule = FModuleManager::GetModuleChecked<FAssetRegistryModule>("AssetRegistry");

for (int i = 0; i < FoundFiles.Num(); i++)
{
	//check for dependencies
	TArray<FName> HardDependencies;
	AssetRegistryModule.Get().GetDependencies(*FoundFiles[i], HardDependencies,EAssetRegistryDependencyType::Hard);

	TArray<FName> SoftDependencies;
	AssetRegistryModule.Get().GetDependencies(*FoundFiles[i], SoftDependencies,EAssetRegistryDependencyType::Soft);

	if (HardDependencies.Num() > 0)
	{
		UE_LOG(LogTemp, Warning, TEXT("hard dependencies found for %s"), *FoundFiles[i]);
	}
	if (SoftDependencies.Num() > 0)
	{
		UE_LOG(LogTemp, Warning, TEXT("soft dependencies found for %s"), *FoundFiles[i]);
	}
	//check for references
	TArray<FName> HardReferecers;
	AssetRegistryModule.Get().GetReferencers(*FoundFiles[i], HardReferecers,EAssetRegistryDependencyType::Hard);

	TArray<FName> SoftReferencers;
	AssetRegistryModule.Get().GetReferencers(*FoundFiles[i], SoftReferencers,EAssetRegistryDependencyType::Soft);

	if (HardReferecers.Num() > 0)
	{
		UE_LOG(LogTemp, Warning, TEXT("hard reference found for %s"), *FoundFiles[i]);
	}
	if (SoftReferencers.Num() > 0)
	{
		UE_LOG(LogTemp, Warning, TEXT("soft reference found for %s"), *FoundFiles[i]);
	}
}

}

1 Like

For those who are still looking for an answer to that old but still valid question.

In AssetDataGatherer there is a flag that controls whether the dependencies must be gathered :

bGatherDependsData = (GIsEditor && !FParse::Param( FCommandLine::Get(), TEXT("NoDependsGathering") )) || FParse::Param(FCommandLine::Get(),TEXT("ForceDependsGathering")); 

if you are outside of the editor, adding the extra -ForceDependsGathering on the command line will do the trick.

1 Like

Hey I know its been a while but do you have more info on how you got that piece of code working?

I’m not sure what the AssetDataGatherer is or how to actually set that flag

Actually no worries, I think I got it sorted.

In your code you are passing FStrings to the various GetDependencies/Referencers calls.
Looking at this piece of the docs:

to me it seemed like we were supposed to passing package names. When I swapped in package names for the fstrings it worked for me.