MetaForge Documentation
Fab Store PageDiscord
  • MetaForge
    • Getting Started
    • Plugin Configuration
    • Runtime Configuration
    • Project Configuration
    • Model Objects
    • Model Managers
    • Registering Models
    • Conditionals
    • Validators
    • References
    • References (Advanced)
    • Property Drawers
    • List Properties
    • IDE File Templates
    • Runtime Usage
  • Contact Us
Powered by GitBook
On this page
  • Overview
  • Defining Validators
  • Example Code
  • Use Cases for Validators
  • Best Practices for Validators
  • Summary
  1. MetaForge

Validators

Overview

Validators in MetaForge are used to ensure that model object properties meet certain criteria before being saved or applied. By using validators, developers can add custom validation logic to properties, ensuring data integrity and preventing invalid configurations in the Unreal Engine editor. Validators help in maintaining a consistent and error-free editing experience.

Defining Validators

To use validators, you can annotate your model object properties using the Validator metadata tag. This tag allows you to specify a function that will be called to validate the property's value. The function must be defined within your class, must be of the form bool FunctionName(FName ContextId), and must be marked as a UFUNCTION to be accessible. The new value to test against can be retrieved from the validator system using the context ID. The type of the parameter is based on the property being validated.

If the property uses an SNameProperty drawer, it will be an FName, for example:

FName newValue = Validator::GetContextParameter<FName>(ContextId);

If the property uses an SIntPropertyDrawer, the value will be an int32, for example:

int32 newValue = Validator::GetContextParameter<int32>(ContextId);

Carefully check the type of the property you are validating when using this.

Example Code

Here is an example of using validators in a model object:

#pragma once

#include "CoreMinimal.h"
#include "UDataModel.h"
#include "UMyModelObject.generated.h"

#if WITH_EDITOR
#include "Metadata/Validator.h"
#endif

UCLASS()
class UMyModelObject : public UObject, public IDataModel
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere, Category = "MyModel", meta = (Validator = "ValidateAdvancedSettingValue", Component = "SFloatProperty"))
    float AdvancedSettingValue;

    UFUNCTION()
    bool ValidateAdvancedSettingValue(FName ContextId)
    {
        #if WITH_EDITOR
        // Ensure that the value is greater than or equal to 0
        float newValue = Validator::GetContextParameter<float>(ContextId);

        if (newValue < 0) {
            ShowErrorNotification("Value must be greater than zero.");
            return false;
        }
        #endif
        return true;
    }
};

In this example:

  • AdvancedSettingValue is a property that requires validation. It is annotated with the Validator tag, specifying the function ValidateAdvancedSettingValue.

  • ValidateAdvancedSettingValue() is a function that checks if AdvancedSettingValue is non-negative. This function ensures that only valid values are assigned to AdvancedSettingValue.

Use Cases for Validators

  • Value Range Enforcement: Ensure that numeric properties fall within an acceptable range, such as positive values or within specific bounds.

  • Dependency Checks: Validate a property based on the value of another property, ensuring that configurations are consistent.

  • Custom Validation Logic: Apply any custom validation logic to enforce project-specific rules and requirements.

Best Practices for Validators

  • Keep Validation Simple: Ensure that validation functions are straightforward and perform minimal computation to avoid slowing down the editor.

  • Use Clear Error Messages: When using validators, provide clear messages to the user to indicate why a value is invalid and how to correct it.

  • Avoid Circular Dependencies: Be careful not to create circular dependencies between properties when defining validators, as this can lead to unexpected behaviors.

Summary

Validators are a powerful tool in MetaForge for ensuring that properties adhere to specific rules before they are saved or applied. By adding validators to your model objects, you can maintain data integrity, prevent invalid configurations, and provide users with a more robust and user-friendly editor experience.

PreviousConditionalsNextReferences

Last updated 6 months ago