Change UProperty exposure in child class

Hey All,

I have an actor with an exposed property, let’s say a float

//in class AMyActor
UPROPERTY(EditAnywhere)
float FloatValue = 0.0f;

so any BP class / instance derived from this class can set another value

what i would like is, in a C++ class AMySpecificActor derived from AMyActor, to force this value and avoid any BP derived class to modify it

I can set FloatValue to whatever I want in AMySpecificActor constructor to have the good default value, but it will still be exposed in the BP editor

I know I can force the value at beginplay

But it would be cleaner to just avoid exposing the value, is there a way to do that ?

1 Like

I don’t think there simple way to do it, all i can think of is change flags of UProperty on runtime and you would need to do this early as possible to make it work, you might try to do this in class constructor which will be called of CDO creation (as well as you spawn it), but im not sure if property data is load up at that point. If not you would need to find different event,

So first get UClass* of class via GetClass() or AMySpecificActor::StaticClass()

From that UClass* do FindPropertyByName(FName("FloatValue")); to get UProperty* of property

And in UProperty you can edit flags wit those functions:

https://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/UProperty/ClearPropertyFlags/index.html
https://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/UProperty/SetPropertyFlags/index.html

And list of flags you will find here:

So you should call ClearPropertyFlags(CPF_Edit); and optionally set SetPropertyFlags(CPF_BlueprintVisible) to make it just visible (atleats i think this is the flag for that, never played with them much)

I also checked, in engine you can find code that does the same thing so i guess it might be ok to do this way:

https://github.com/EpicGames/UnrealEngine/blob/7d9919ac7bfd80b7483012eab342cb427d60e8c9/Engine/Source/Developer/Windows/LiveCoding/Private/LiveCodingSettings.cpp

I recommend to this inside:

#if WITH_EDITOR

//code here

#endif

So this code only compile in editor builds, just to make sure the game will package and run as things may not be the same without editor code.

Other hack i can think of (figured mid way of writing this and may be less invasive ;p) is use:

UPROPERTY()
bool bCanEditFloatValue;

 UPROPERTY(EditAnywhere, meta(EditCondition=bCanEditFloatValue))
 float FloatValue = 0.0f;

I nto sure if this gonna work without bCanEditFloatValue exposed, but set bCanEditFloatValue to false on constructor and since bCanEditFloatValue this should make FloatValue permanently disabled (atleast in property editor)

2 Likes

Thanks for the quick answer, SetPropertyFlags was what I was looking for, I guessed the UE reflection system was allowing us to do so but I missed the starting point that was FindPropertyByName
Thanks for all the details !

Note that using the ClearPropertyFlags(CPF_Edit) approach it ends up hiding the property for the superclass as well :frowning:

2 Likes