ClampMin and ClampMax meta for specific UFUNCTION arguments?

hi there…

I, f.e. got his function:

UFUNCTION(BlueprintCallable, BlueprintPure=false, Category="Testing")
void DoThings(int A, int B, int C, FVector3d& ReturnPoint);

I now want to have B, and only B, being clamped between 5 and 50…
Since there is no way to tell the UFUNCTION, which argument the meta should affect, and UPROPERTY won’t help either, I searched a while through Google, stackoverflow and diverse UE coding pages…

I found UPARAM… and tried it…
This is the result:

UFUNCTION(BlueprintCallable, BlueprintPure=false, Category="Testing")
void DoThings(int A, UPARAM(meta=(ClampMin="5", ClampMax="50"))int B, int C, FVector3d& ReturnPoint);

And it is doing absolutely… nothing… it just won’t clamp the value of B (in the BP Node) between 5 and 50… I can put -8498 or 999 or anything else inside… and it won’t get clamped…

How can one clamp a specific argument?

I don’t think that would be possible
You can use the clamp on a uproperty and use that, or you’d have to clamp the value inside the function.

1 Like

Hi @BDC_Patrick

Like @FishBone0 said you can clamp inside the function:

UFUNCTION(BlueprintCallable, BlueprintPure=false, Category="Testing")
void DoThings(int A, int B, int C, FVector3d& ReturnPoint);
...
void AYourClase::DoThings(int A, int B, int C, FVector3d& ReturnPoint)
{
A = FMath::Clamp(A, 5.f, 50.f);
...
}

In addition to clamping in the function itself, you can also implement OnPropertyChanged and force it to remain clamped when set… that way you don’t have it appearing to be 0 in editor, but it being clamped to 5 in runtime, or whatever.

No need to use OnPropertyChanged, as the property itself can be clamped with meta=(ClampMin="5", ClampMax="50") in the UPROPERTY specifier

which is not eorking for a UFUNTION… since the meta is not knowing which argument is meant to be clamped…

would be nice if epic implements such a thing… so we have a MinMax Slider in the Node

Seems like a bit of a case of over-engineering.


2

It’s a function. You need to pass in it’s limits, otherwise why not just bake them inside the function as local variables and clamp the value?
If you want cleaner inputs then you can also go for a FVector2d to pass in min and max in the same line.

The clamp meta is only useful for property editors when designing / configuring from the editor panel. That meta does not even exist without the editor. They can not be used for proper validation and will not automatically clamp a value when you set a property from code. The easiest way to set up value validation is to make your properties private and validate through public setter methods. In the case of functions just validate the arguments within the function and print a warning when something is not what you expect, or clamp a value if that is what you wish, or assert if an input may never be a wrong value.