Property Specifier BlueprintGetter and BlueprintSetter Seems Useless

The source code is at the bottom.

If I don’t use the property specifier BlueprintGetter and BlueprintSetter for the private variable Health , I can also use Getter and Setter function to access it in Blueprint, like this:

GetterAndSetter

But if I delete the comment and use the specifier, I can access the private variable Health directly in Blueprint.Doing so destroys encapsulation, which is the opposite of the purpose of Getter and Setter functions.

GetterAndSetter2

So what’s the meaning of BlueprintGetter and BlueprintSetter ?

private:
    // UPROPERTY(BlueprintGetter=GetHealth, BlueprintSetter=SetHealth) ?????
    int32 Health;
public:
    UFUNCTION(BlueprintPure)
    int32 GetHealth() const { return Health; }
    UFUNCTION(BlueprintCallable)
    void SetHealth(int32 InHealth) { this->Health = InHealth; }

There is a old post about this topic, but I think their answer didn’t solve my problem, which is that the property specifier BlueprintGetter and BlueprintSetter aren’t actually needed at all.

I already know the answer. What these two property specifier do is like specifier ReplicatedUsing. They make marked function as a callback function. No matter what approach the BP side access the property,those access function will always be called.

public:
	UFUNCTION(BlueprintPure)
    int32 GetHealth() const
	{
		UE_LOG(LogTemp, Warning, TEXT("GetHealth: %d"), Health);
		return Health;
	}
    UFUNCTION(BlueprintCallable)
	void SetHealth(int32 InHealth)
	{
		UE_LOG(LogTemp, Warning, TEXT("SetHealth: %d"), InHealth);
		Health = InHealth;
	}
private:
	UPROPERTY(BlueprintGetter=GetHealth, BlueprintSetter=SetHealth)
    int32 Health{100};

We can get log info like this:

LogTemp: Warning: SetHealth: 50
LogTemp: Warning: GetHealth: 50

1 Like