This page shows an example of how you can use Editor Validators to validate Blueprint actor assets and their settings.

This was one of the primary usecases I wanted in my own projects: Certain actors need their components configured in a particular way to work correctly in my game. We can validate this with Editor Validators which will automatically show a warning for incorrectly configured actors, although it has a small gotcha which I will explain at the end.

Creating an editor validator

//.h file
UCLASS()
class MY_API UMyValidator : public UEditorValidatorBase
{
	GENERATED_BODY()
 
public:
	virtual bool CanValidateAsset_Implementation(const FAssetData& InAssetData, UObject* InObject, FDataValidationContext& InContext) const override;
 
	virtual EDataValidationResult ValidateLoadedAsset_Implementation(const FAssetData& InAssetData, UObject* InAsset, FDataValidationContext& Context) override;
};
 
 
//.cpp file
bool UMyValidator::CanValidateAsset_Implementation(const FAssetData& InAssetData, UObject* InObject, FDataValidationContext& InContext) const
{
	if(auto* BP = Cast<UBlueprint>(InObject))
	{
		return BP->ParentClass->IsChildOf<AItemBase>();
	}
	
	return false;
}
 
EDataValidationResult UMyValidator::ValidateLoadedAsset_Implementation(const FAssetData& InAssetData, UObject* InAsset, FDataValidationContext& Context)
{
	auto* BP = CastChecked<UBlueprint>(InAsset);
	if(auto* Item = Cast<AItemBase>(BP->SimpleConstructionScript->GetComponentEditorActorInstance()))
	{
		//check something...
		AssetFails(InAsset, INVTEXT("My error message"));
		return EDataValidationResult::Invalid;
	}
 
	AssetPasses(InAsset);
	return EDataValidationResult::Valid;
}

Couple things worth noting here:

We use CanValidateAsset_Implementation to choose which assets to validate. Because we want to validate blueprint actor assets, the asset type is a UBlueprint, and we can verify its type using IsChildOf on the ParentClass.

Next, we use ValidateLoadedAsset_Implementation to perform the validation. Note that we again need to cast to UBlueprint. The gotcha I mentioned at the start comes here.

To validate components added in the blueprint subclass, we need to get a “constructed” instance of the class, so that it has all the BP-added parts. We can’t perform validation logic on the components otherwise. To get a useful instance, we can use BP->SimpleConstructionScript->GetComponentEditorActorInstance(). This returns the actor instance used by the UE editor when editing the blueprint.

The gotcha is this: The editor actor instance only exists if the blueprint editor is open. This means the validator will not work when manually running Validate Asset, and it will only work when saving the actor after editing it. This shouldn’t be a problem, since it will output any errors or warnings from saving it, but it’s something to keep in mind. If you know a better way of getting a list of components that don’t have this problem, I’d love to know (Contact)

An alternative approach to using the editor actor instance is loading blueprint CDO components. This works when using Validate Assets without having the actor open in an editor, but for example any component’s physics state might not be available.