Can UPROPERTY be a private member?

Below is an example header file of using UPROPERTY on a private member variable.

This header file seems to work, but I did run into a one-time bug where this particular component stopped saving changes to anything in the details window (the blueprint compile button wouldn’t change to “needs to be compiled” regardless of any setting changed on this component). Deleting the component and re-adding it fixed the problem and changes could be made again, but I wasn’t able to reproduce the bug after that. Since I couldn’t reproduce the bug, I couldn’t deduce if using UPROPERTY on the private member was the cause or not.

So I’m wondering: Is it safe to use UPROPERTY on a private member or do they have to be protected instead?

#pragma once

#include "CoreMinimal.h"
#include "Components/StaticMeshComponent.h"
#include "TankBarrel.generated.h"

UCLASS(meta = (BlueprintSpawnableComponent))
class BATTLETANK_API UTankBarrel : public UStaticMeshComponent
{
	GENERATED_BODY()
	
public:

	void Elevate(float relativeSpeed);
	
private:
	UPROPERTY(EditAnywhere, Category = Setup)
		float MaxDegreesPerSecond = 20; 

	UPROPERTY(EditAnywhere, Category = Setup)
		float MaxElevationDegrees = 40;

	UPROPERTY(EditAnywhere, Category = Setup)
		float MinElevationDegrees = 0; 
	
};

I have not seen any explicit call outs as to the access modifiers when referencing the UHT macros and I have successfully used both UPROPERTY and UFUNCTION on private members so I think you end up just fine due to the magic of the header tool and the generated class.

That said I have had several occasions where updating the UPROPERTY or UFUNCTION (anything UHT related really) code can cause some weird/undefined behavior. That seems to happen, once in a while, if the engine is up and running and there is an instance of my object or a descendant of my object in the level. When that happens it always clears up for me by just restarting the engine. This does not seem to always happen to me when updating existing object code for sure (which is impressive if you think about it), but it does happen.

I know this is a quite old thread. EditAnywhere seems to be save for private members that are exposed to BP. I use them very often for settings of a object.

Additionally, we can use

UPROPERTY(..., meta = (AllowPrivateAccess = "true"))

To get access for Blueprints.

1 Like