Hi Unreal developers,
I am pondering about the best practices concerning visibility with class members/properties and how to achieve data encapsulation in UE for a while now.
In the examples (like Shooter Game or Strategy Game), many times public members are used. When a member needs to be a UPROPERTY, this is even enforced now: a private UPROPERTY leads to a compiler error (“BlueprintReadOnly should not be used on private members”). This makes sense, since defining it as UPROPERTY looks like the equivalent of making the member public to the Unreal Editor, as far as I understand it. But making the member protected does not produce any error. Why is that? protected basically works like private in making it non-public for other classes not inheriting from the one defining the members.
Aside from that, in a recent post I learned that the recommended way of adding components to classes now is to make them private but readable by Blueprints. This can be achieved using the meta specifier “AllowPrivateAccess”, which disables the compiler error. It seems like a (dirty?) workaround for stuff like components to compromise the visibility at least for the Editor (couldn’t find any documentation sadly). I guess it should not be used frequently to have private members with getters/setters in C++ and directly writable properties in the Editor, at least that approach sounds inconsistent to me.
Am I right in the assumption that the best way to enforce information hiding in UE would be to have private members and getters/setters that are BlueprintCallable UFUNCTIONs so they have to be called from the editor to edit the value?
Or is this not worth the effort? (I’m getting the suspicion I’m being overzealous about best practices here since I see public members used so many times in the examples)
One drawback with using getters/setters is, that I think I can’t use archetype defaults then since the member is not directly editable by the Editor.
What’s your opinion or experience on public members vs private members with getters/setters in UE?