Data Asset: Only allow an int var to be positive

Fairly simple question, but I can’t tell if I’m just not finding documentation or if I shouldn’t be trying to do this in the first place.

In my C++ UDataAsset, I have the uproperty:

int32 NumPages = 100;

I want to make sure the input for this data asset is always positive using a MakePositive function (which is already written.)

But… how do I do that? Is it even a good idea to do that or is this not how a data asset is intended to work?

If you’re trying to limit the value of NumPages to only positive values while modifying it in the edttor, you can use the meta property ClampMin to set a minimum value.

UPROPERTY(EditAnywhere, BlueprintReadOnly, meta=(ClampMin=0))
int32 NumPages = 100;

There’s also the PostEditChangeProperty function that you can override to do operations on the value after it was edited.

#if WITH_EDITOR
void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
#if WITH_EDITOR
void ASomeActor::PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent)
{
  Super::PostEditChangeProperty(PropertyChangedEvent);

  FName PropertyName = (PropertyChangedEvent.Property != NULL) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
  if (PropertyName == GET_MEMBER_NAME_CHECKED(ASomeActor, NumPages)) 
  {
    if (NumPages < 0) 
    {
        MakePositive();
    }
  }
}
#endif

^ modified code from: