What's the point of BlueprintGetter if it didn't hide the direct access to that variable in BP?

define in code:

UPROPERTY(BlueprintGetter = Getter)
int var;

UFUNCTION(BlueprintCallable)
int Getter() const;

But I can still get the value of var by a direct reference to it in BP.

Because it is implicitly BlueprintReadOnly.

BlueprintGetter=GetterFunctionName

This property specifies a custom accessor function. If this property isn’t also tagged with BlueprintSetter or BlueprintReadWrite, then it is implicitly BlueprintReadOnly.

Similarly with BlueprintSetter.

So I don’t understand the purpose of designing these two Specifiers…

2 Likes

You usually implement getters and setters for the variables that are private.

Yes, but for BlueprintReadOnly property, private doesn’t prevent access from BP. (I’v edited the title.)

The node looking like direct access to the property now executes as your Getter method instead of directly getting the value. The phrase it is implicitly BlueprintReadOnly means that the setter won’t be accessible, while getter is your getter function.
Sorry for the late answer.

BlueprintGetter can be used when your member variable has private member access in C++ but you want to expose it to Blueprints.

In general you keep a member variable as private if you don’t want to give direct access to it (neither from C++ nor from Blueprints), but want callers (also from C++) to use the getter/setter methods. Such methods are useful - for example - to run checks on managed values, fire delegates on changes, add logging, modify later where/how the variable is stored (or how it’s computed), etc. You can read more about it here on Wikipedia.

Trying with the ``BlueprintReadOnly` specifier:

private:
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
    FText var;

causes UHT to raise an error :

Error: BlueprintReadOnly should not be used on private members

Using BlueprintGetter instead, you can keep your variable private in C++ but exposing it to your Blueprints through the specified method:

private:
    UPROPERTY(EditDefaultsOnly, BlueprintGetter=Getter)
    FText var;
2 Likes

That’s a really bad way to just expose private variable. You have a meta=(AllowPrivateAccess=true) for that.

1 Like

The point of having a private member variable is that you don’t want to give direct access to it to other classes, including blueprints. Otherwise you would leave it as public. Using AllowPrivateAccess, while useful if you have old code where to moved a property from public to private, in general would invalidate this pre-condition. Having getter/setter instead allows your to run checks on returned and new values, fire delegates on value changes, add logging, change how/where the property is stored (or computed), etc. So, using BlueprintGetter and BlueprintSetter simply support this programming model, of course if this is what you want/need.

(I updated the answer with the above explanation)

1 Like

Setter is for when you want to be able to react whenever a blueprint designer sets your variable. For example in multiplayer online game you might want to call OnRep function for that variable being set, since you have to do it manually. You can simply get info back to code whenever a blueprint user sets or gets the variable and react to it however you want

2 Likes