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;

    #if WITH_EDITOR
        virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
    #endif
    

And in the source file:

#if WITH_EDITOR
void AFPCPPCharacter::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	Super::PostEditChangeProperty(PropertyChangedEvent);   

	DefaultHealth = FMath::Clamp(DefaultHealth, 0, MaxHealth);
}
#endif

This is what happens:

By default, DefaultHealth is clamp to 100:

308139-1.jpg

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

308140-2.jpg

Is this what you want?

4 Likes

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 https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1392262-with_editor-vs-with_editoronly_data-need-clarification 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.