ClampMax Based on other variable value

Is there a way to use ClampMax value based on variable’s value instead of hardcoded number while declaring a variable in C++ header.Something like following

//Default Health of an Actor 
UPROPERTY(EditAnywhere, Category = "Health",meta=(ClampMax="MaxHealth"))
int32 DefaultHealth = 100;

//Maximum Health of an Actor 
UPROPERTY(VisibleAnywhere, Category = "Health")
int32 MaxHealth = 100;

This gives me error. Although This can be handled in begin Play function but I am looking for more user friendly way.

I have gone through documentation but couldn’t find anything related to it in Metadata Specifiers | Unreal Engine Documentation

1 Like

And the error is …

error : Metadata value for ‘ClampMax’ is non-numeric : ‘MaxHealth’

Are you trying to redefine the DefaultHealth max value based on MaxHealth? I mean, if you define a new value for MaxHealth in the editor, then DefaultHealth will be clamp to that value. Is that what you want?

If so, then 2 things:

  1. the way MaxHealth is defined, you cannot change its value in the editor. So, I’m confused.

  2. I don’t think you can use meta to define the limit based on other variable. You can however use PostEditChangeProperty, for that:

    //Default Health of an Actor 
    UPROPERTY(EditAnywhere, Category = "Health")//, meta = (ClampMax = 100))
    int32 DefaultHealth = 100;

    //Maximum Health of an Actor
    //UPROPERTY(VisibleAnywhere, Category = “Health”)
    UPROPERTY(EditAnywhere, Category = “Health”)
    int32 MaxHealth = 100;

        virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;

And in the source file:

void AFPCPPCharacter::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)

	DefaultHealth = FMath::Clamp(DefaultHealth, 0, MaxHealth);

This is what happens:

By default, DefaultHealth is clamp to 100:


But if you change MaxHealth in the editor to 200, then DefaultHealth is now clamp to 200:


Is this what you want?


Yes that’s what exactly I want. Thank you for taking the time out :slight_smile: One More question what is WITH_EDITOR macro?

There are 2 macros closely related with each other: WITH_EDITOR AND WITH_EDITORONLY_DATA. Check for a better understanding.

Thank you for the hint!

I would really like to be able to specify meta=, this is the only way to obtain a slider as far as I know.