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
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:
-
the way MaxHealth is defined, you cannot change its value in the editor. So, I’m confused.
-
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:

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

Is this what you want?
2 Likes
Yes that’s what exactly I want. Thank you for taking the time out
One More question what is WITH_EDITOR
macro?