Code before ObjectTools::DeleteAssets does not get called

Hi
I am trying to delete a selected asset, however I want to call some code before the ObjectTools::DeleteAssets function, but they are not executed until after. I am assuming it’s because of multithreading, any ideas how I can fix this?

void MyFunc()
	{
		myDelegate.Execute(); // Gets called last
		UE_LOG(LogTemp, Warning, TEXT("Delegate called"));  // Gets called last

	
		TArray<FAssetData> AssetToDelete;
		AssetToDelete.Add(*SelectedAsset.Get());

		ObjectTools::DeleteAssets(AssetToDelete); // Gets called first
	}

Don’t know the exact answer for your question (sadly),
but if you have access to the UWorld from that function’s scope, you might be able to delay the call of ObjectTools::DeleteAssets

e.g.

void MyFunc()
{
	myDelegate.Execute();
	UE_LOG(LogTemp, Warning, TEXT("Delegate called"));

	GetWorld()->GetTimerManager().SetTimerForNextTick(
		[&]()->void
		{
			TArray<FAssetData> AssetToDelete;
			AssetToDelete.Add(*SelectedAsset.Get());
		
			ObjectTools::DeleteAssets(AssetToDelete);
		}
	);
}

Of course, this is not an optimal solution since it doesn’t really fix the underlaying issue, it’s only a workaround.

The main problem is because ObjectTools::DeleteAssets creates a modal window, that blocks the main thread until the window is closed, so rather than using that, I used a FMessageDialog, not as pretty but gets the job done, then I simply delete with UEditorAssetLibrary::DeleteAsset

You can also call ObjectTools::DeleteAssets without triggering the modal window.
Just add false as a 2nd argument:

ObjectTools::DeleteAssets(AssetToDelete, false);

from the source:

/**
* Deletes the list of objects
*
* @param	AssetsToDelete		The list of assets to delete
* @param	bShowConfirmation	True when a dialog should prompt the user that they are about to delete something
*
* @return The number of assets successfully deleted
*/
UNREALED_API int32 DeleteAssets( const TArray<FAssetData>& AssetsToDelete, bool bShowConfirmation = true );

Good to hear that you’ve found the solution.

I did try that, but it still blocks the thread. I went about it wrong from the start anyway, if I was to use the object tools delete function, I needed the code to run once the delete button was clicked then the asset get’s called, but that isn’t possible I don’t think. So using the message dialog meant that, I check if the yes to delete is clicked then I run the code then delete the asset, else do nothing.

The object tool looks better than the message box but I can’t run the code at the point I want to. which also removes the pointless array, since I only allow for one asset to be deleted at time.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.